Context Object Manipulation

Last Updated on: September 15, 2022

One of the key concepts of JavaScript is the keyword this, which signifies the JavaScript context object.

If you write an inner function, you can access any local variables you define in the outer function exactly as if you had defined them locally. You can also access any arguments you pass to a function as if they were local variables.

When you call a function, this is the window. It is also the window in the global context.

var myval = function () {return this};
myval() == window;

You can send other objects as the context object into a function in two ways:

  • Call a function as a member of an object
  • Use the first argument of the method

This is an example of the first method:

var myval = {‘myval2’:myval2};
myval.myval2() == myval;

This example demonstrates the second method:, 0, 1, 2, ...);

You can then use any arguments you want to pass as you would normally.

Consider this more complex example:

Function outFunc (outArg) {
  Var localvar = 100;

  Function inFunc(inArg) {
    Return (outArg + inArg + localVar);

  Return inFunc;

var globalVar = outerFunc(200);
alert(globalVar(300)); // Displays 700
alert(globalVar(300)); // Displays 800
globalVar = outerFunc(200)
alert(globalVar(300)); // Displays 700 again

The execution context is a set of objects with properties that is not publicly accessible and executes the JavaScript code.

The code relies on the execution context for many things, including variable resolution and scooping. All global code runs in a global execution context, typically the window object. Every time you call a function, it has an associated execute context for the duration of the function call.

After the function returns, the execution context is available for garbage collection.

Each new execution context creates an activation object. This is a normal JavaScript object with accessible named properties but no prototype, and you cannot reference it directly with JavaScript code.

The activation object stores context-related information, like call arguments and variables.

When you execute a JavaScript function, the following steps occur:

  • Create activation object
  • Create arguments object for activation object
  • Assign local variables and declarations
  • Assign this keyword
  • Assign scope and scope chain to the execution context
  • Call function and execute the code
  • Returns and deletes any created objects.

Therefore, each function keeps open until it is closed, and all variables in any context retain any assigned values until they are assigned a different value.

Get notified of new posts:

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *