This is the case for JavaScript code that is evaluated when a <script> element is encountered:
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)).
§15.1.2.1.1 defines what a direct call to eval() is. Basically, eval(...) is a direct call whereas something like (0, eval)(...) or var indirectEval = eval; indirectEval(...); is an indirect call to eval(). See chuckj's answer to (1,eval)('this') vs eval('this') in JavaScript? and this blog post by Dmitry Soshnikov for when you might use an indirect eval() call.
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:
Solution 2:
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:
Solution 3:
Solution 4:
global this
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
function this
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.
prototype 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.
object this
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
HTML this
Inside HTML attributes in which you can put JavaScript, this is a reference to the element.
eval this
You can use eval to access this.
with 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.
jQuery this
the jQuery will in many places have this refer to a DOM element.
Related Searches to javascript tutorial - “this” keyword
this in javascript functionthis keyword in javascript w3schoolsthis javascript w3schoolsjavascript this scopejavascript this onclickexplain how prototypal inheritance workswhat is meant by this keyword in javascript mcq$this jquerycall javascript function from htmljavascript function callthis keyword in javascript w3schoolsjavascript apply examplejavascript this onclickjavascript call vs applycall javascript function in html bodycalling external javascript function from htmljavascript tutorialjava scriptjavascriptjavascript arrayjavascript booklearn javascriptjavascript codejavascript editorjavascript classjavascripjavascript debuggerjavascript onlinejavascript examplesjavascript testjavascript documentjavascript sliderwhat is javascriptjavascript form validationjavascript validatorhtml javascriptjavascript alertjavascript eventsjavascript printjavascript domjavascript objectjavascript functionhref javascriptjavascript datejavascript promptjavascript onclickjavascript returnjavascript forjavascript numberjavascript confirmjavascript onchangejavascript regular expressionjavascript ifjavascript variablejavascript timerjavascript cookiejavascript getelementbyidjavascript innerhtmljavascript calljavascript regexpjavascript includesjavascript thisjavascript eval