This keyword is the window object inside the constructor function.

Okay, so I thought I figured it out (pun intended), but apparently not.

var Constructor = function () { var internalFunction = function () { return this === window; }; this.myMethod = function () { alert(internalFunction()); }; }; var myObj = new Constructor(); myObj.myMethod(); 

This warns true . Why can't an internal function see this as an object? Instead, I should use alert(internalFunction.call(this)); in myMethod .

Edit: I was looking for an explanation as to why this is assigned this way, and not workarounds like var self = this; etc. Sorry if I didn’t make it clear.

+7
source share
4 answers

this not bound until the function is called and independent of how the function is called. You might think of it as an extra parameter implicitly passed to the function.

In this case, the problem is that you are calling internalFunction with internalFunction() . This value is set either by calling the function as a method (as in foo.bar() , or foo["bar"]() ), or by setting this explicitly via call() or apply() . Your call does not make this return to the global object.

The easiest way to achieve what you want in this case, while keeping internalFunction private, is to keep a reference to this inside the constructor function:

 var Constructor = function() { var thisObj = this; var internalFunction = function () { return thisObj === window; }; thisObj.myMethod = function () { alert(internalFunction()); }; } 
+6
source

Due to the functional rules for determining the scope, this reassigned within each function ... I would save a copy of your object as self and use it accordingly ...

 var Constructor = function () { var self = this; var internalFunction = function () { return self === window; }; this.myMethod = function () { alert(internalFunction()); }; }; var myObj = new Constructor(); myObj.myMethod(); 

Should give you the expected result.

Sidenote

This is a pretty dangerous practice created by javascript, mainly because if you forget the new keyword when using the Constructor , you will get this , referring to the window (god) so that you don't attach myMethod to the window without warning.

+3
source

There are five ways to call a function in JavaScript. The value of this depends on the option you select:

  • A global function call (e.g. myFunction() ). No explicit value is specified for this . The value of this will be the default object ( window in the browser).
  • Method call (e.g. obj.myFunction() ). The value of this is the object on which the method was called ( obj in this case).
  • Using the call method (e.g. myFunction.call(obj) ). The value of this provided explicitly (in this case obj ).
  • Using the apply method (e.g. myFunction.apply(obj) ). The value of this provided explicitly (in this case obj ).
  • Constructor function (e.g. new MyFunction() ). The value of this is the newly created object provided by the runtime.

Each of the five is explained in more detail here:

+2
source

His scope problem will try something like:

 var Constructor = function () { var $this = this; var internalFunction = function () { return $this === window; }; this.myMethod = function () { alert(internalFunction()); }; }; var myObj = new Constructor(); myObj.myMethod(); 
+1
source

All Articles