When evaluating code in the initial global execution context, ThisBinding is set to the global object, window (§10.4.1.1).
Entering eval code
... by a direct call to eval()
ThisBinding is left unchanged; it is the same value as the ThisBinding of the calling execution context (§10.4.2(2)(a)).
... if not by a direct call to eval()
ThisBinding is set to the global object as if executing in the initial global execution context (§10.4.2(1)).
Entering function code
This occurs when calling a function. If a function is called on an object, such as in obj.myMethod() or the equivalent obj["myMethod"](), then ThisBinding is set to the object (obj in the example; §13.2.1). In most other cases, ThisBinding is set to the global object (§10.4.3).
The reason for writing "in most other cases" is because there are eight ECMAScript 5 built-in functions that allow ThisBinding to be specified in the arguments list. These special functions take a so-called thisArg which becomes the ThisBinding when calling the function (§10.4.3).
These special built-in functions are:
So here, inside each(), "this" doesn't hold the "value" that you expect it to (from
above it). So, to get over this (no pun intended) problem, a developer could:
In a browser, at the global scope, this is the windowobject
In node using the repl, this is the top namespace. You can refer to it as global.
In node executing from a script, this at the global scope starts as an empty object. It is not the same as global
Except in the case of DOM event handlers or when a thisArg is provided (see further down), both in node and in a browser using this in a function that is not called with new references the global scope…
If you use use strict;, in which case this will be undefined
If you call a function with new the this will be a new context, it will not reference the global this.
Functions you create become function objects. They automatically get a special prototypeproperty, which is something you can assign values to. When you create an instance by calling your function with new you get access to the values you assigned to the prototype property. You access those values using this.
It is usually a mistake to assign arrays or objects on the prototype. If you want instances to each have their own arrays, create them in the function, not the prototype.
You can use this in any function on an object to refer to other properties on that object. This is not the same as an instance created with new.
DOM event this
In an HTML DOM event handler, this is always a reference to the DOM element the event was attached to
Unless you bind the context
You can use eval to access this.
You can use with to add this to the current scope to read and write to values on this without referring to this explicitly.
the jQuery will in many places have this refer to a DOM element.
Wikitechy Rated 5 / 5 based on 378284 reviews.
World's No 1 Animated self learning Website with Informative tutorials explaining the code and the choices behind it all.