Types

JavaScript variables are either Objects (including functions) or one of the primitive types: Boolean, Number, String, null or undefined.

Objects may have prototypes. Prototypes are objects which themselves may have prototypes, forming the finite-length prototype chain. An object with a null prototype ends the prototype chain (Object.prototype has a null prototype).

When a member is accessed via the dot operator, each prototype in the prototype chain is checked in turn, until the named member is found, or the null prototype is reached, in which case undefined is returned. The internal [[prototype]] member, referring to an object's prototype, is not publicly accessible.

Inheritance and shared members are implemented using prototypes in JavaScript. The non-standard, settable __proto__ property can be assigned an object to cause that object to inherit from the assigned object.

While a particular method is being executed, the this keyword refers to the object on which the dot operator was applied. This means that in an inherited method it still refers to the subclass. this always refers either to (1) the global object (which is window in a browser) outside a function or inside a function invoked via a variable, (2) the owner of a property access, (3) the value of the first argument passed into Function.prototype.call/apply, (4) the newly created object in a constructor, (5) the calling context's this in evaled code.

Function.prototype.bind takes an object and returns a function which, when invoked, will have a this value equal to the object passed to bind.

To create objects with the same structure but different state, we use constructors.

During JavaScript execution, a stack of execution contexts is created. Specifically, global code gets an execution context, and each invocation has an associated execution context. evaled code also has a distinct execution context. When a function returns, the current execution context is popped from the stack. When an execution context is created, the following takes place:

  1. A special Activation object is created. This has no prototype, but does have accessible named properties.
  2. An arguments object is created. This maps integer indices onto the corresponding actual parameters of the function, and has callee and length properties.
  3. The context is assigned a scope chain. Each function object has an internal property, [[scope]], containing a list of objects. The scope for the new execution context consists of the scope chain of the function object under execution with the newly-created Activation object prepended.
  4. The Activation object is also a Variable object. This contains properties for each of the function's formal parameters, assigned the the values of the actual parameters (or undefined when not present). Any inner functions create function objects that are kept in this Variable object. Finally, local variables declared in the function are stored in the variable object in properties according to their names. The value of a local variable is only assigned during execution of the relevant line of the function body (taking into account hoisting), but is initially undefined.
  5. The this keyword is assigned. If the assigned value is null, property accesses refer to the global object.

The global execution context does not have an arguments property, but its variables object is created in the normal way, including 'local' variables and function definitions, which appear as global variables and top-level functions.

Closures

Statements in inner function bodies may access local variables, parameters and declared inner functions within their outer functions. When it is made accessible outside the function where it is declared, a closure is formed, and it continues to have access to those variables.