has been to JS this does not understand, recently read the essence of the JavaScript language related chapters, a little clear understanding of the record
/* From: The essence of JavaScript language method: When a function is defined as a property of an object, we call it a method. This: When the function accesses this, the this is bound to the global object. *///global function var log = function (msg) { Document.writeln ("<br>" + msg + "<br>");}; Log ("**************thistest.js***************");//define a global variable var value = 100;//function var add = function (A, b) { var value = One by one; Access internal variable log ("Add:value =" + value); Output Add:value = one //When accessing this, the this binding global object, such as This.value, can access the value log defined above ("Add:this.value =" + This.value); Outputs A + b;} Add is a function that accesses this in add, and this binds to the global object Add (1, 2);//output://add:value = 11//add:this.value = 100
The following defines an object
Object var myObj = { ///object property value:0, //Method increment:function (inc) {//when accessing this at this time, the MYOBJ object of this binding log ("Myobj.increment (): Myobj.value =" + this.value);//output Myobj.value value //Call value Increment 1 this.value + = typeof inc = = = ' number '? Inc:1; }};
Sometimes we define a function inside a method, so how does the function call the object property, if the function content directly with this is not access to the object's
Object var myObj = { ///object property value:0, //Method increment:function (inc) {//when accessing this at this time, the MYOBJ object of this binding log ("Myobj.increment (): Myobj.value =" + this.value);//output Myobj.value value //Call value Increment 1 this.value + = typeof inc = = = ' number '? Inc:1; Defines an intrinsic function, or a function, so that the inside of the function accesses this or is only accessible to the global object var infun = function () { //At this time when accessing this, the this binding global object, such as This.value You can access the value log defined above ("MyObj.increment.inFun:this.value =" + This.value); }; Infun (); }};
In order to access the MyObj object within the Infun function, we first save the MyObj object in a variable,
Object var myObj = { ///object property value:0, //Method increment:function (inc) { ///To save the current object MYOBJ reference in the self variable, The subsequent function can access self to replace the Access MyObj object var self = this; At this time, when this is accessed, the this binds the MYOBJ object log ("Myobj.increment (): Myobj.value =" + this.value);//output Myobj.value value //Call once Value increased by 1 this.value + = typeof Inc = = = ' number '? Inc:1; Defines an intrinsic function, or a function, so that the inside of the function accesses this or is only accessible to the global object var infun = function () { //At this time when accessing this, the this binding global object, such as This.value You can access the value log defined above ("MyObj.increment.inFun:this.value =" + this.value); log ("MyObj.increment.inFun:myObj.value =" + Self.value); }; Infun (); }};
Add a double method to MyObj
myobj.double = function () { var = this; var helper = function () { //This can not be accessed with This.value to Myobj.value value //add before the This may be omitted, or can be used This.add (A, b); Self.value = Add (Self.value, self.value); }; Helper ();};
Double the value of Myobj.value by calling the Double method
The understanding of JavaScript this