var o={}; o.aa = function (){ var that = this; console.log(this); function help(){ console.log(that); } help(); } o.aa();
var o={}; o.aa = function (){ var that = this; console.log(this); function help(){ console.log(that); } help(); } o.aa();
var o={}; o.aa = function (){ var that = this; console.log(this); function help(){ console.log(that); } help(); } o.aa();
var o={}; o.aa = function (){ var that = this; console.log(this); function help(){ console.log(that); } help(); } o.aa();
var o={}; o.aa = function (){ var that = this; console.log(this); function help(){ console.log(that); } help(); } o.aa();
var o={}; o.aa = function (){ var that = this; console.log(this); function help(){ console.log(that); } help(); } o.aa();
var o={}; o.aa = function (){ var that = this; console.log(this); function help(){ console.log(that); } help(); } o.aa();
var o={}; o.aa = function (){ var that = this; console.log(this); function help(){ console.log(that); } help(); } o.aa();
var o={}; o.aa = function (){ var that = this; console.log(this); function help(){ console.log(that); } help(); } o.aa();
var o={}; o.aa = function (){ var that = this; console.log(this); function help(){ console.log(that); } help(); } o.aa();
Run the two example respective, you will find the javascript language implement the "Function Invocation Pattern" with it's quirk.
Quote like the JavaScript the Good Part:
When a function is invoked with this pattern, this is bound to the global object. This was a mistake in the design of the language. Had the language been designed correctly, when the inner function is invoked, this would still be bound to the this variable of the outer function. A consequence of this error is that a method cannot employ an inner function to help it do its work because the inner function does not share the method's access to the object as its this is bound to the wrong value. Fortunately, there is an easy workaround. If the method defines a variable and assigns it the value of this, the inner function will have access to this through that variable. By convention, the name of that variable is that:
first example:
var o={};
o.aa = function (){
console.log(this);
function help(){
console.log(this);
}
help();
}
o.aa();
second example:
var o={};
o.aa = function (){
var that = this;
console.log(this);
function help(){
console.log(that);
}
help();
}
o.aa();