About this point in the summary js

js point in this issue has been a pit before been a muddle, probably know little, but did not know what point the difference in each case, today hands at this point of the test.
1. In the object of the this
the this object points to the object we created, for example:

var obj ={
    ccc : 1122,
    ddd : 2233,
    ded : function(){
            console.log(this);
        }  ,
    fff : function(){
            console.log(this === obj );
        }
}
obj.ded();
obj.fff();

The answer executed in chrome as follows:
clipboard.png

The figure we can see that when we execute ded function under the object obj, print out all the properties of matter inside the Object object, showing the object

clipboard.png

Can see the object obj contains methods and properties are included in this object below, then run fff function, the object obj, and this comparison, it can be seen that the two are identical, so that this point in the object's object .

2. this point when the direct call function
to create a function:

function main(){
    this.aad = 234;
    console.log(this);    
}
main();

Operating results are as follows:
clipboard.png

We can see the print out of this object points to the global variable window, created aad variable contains an object directly below the window, so when this function is called directly pointing the window object.

3. The constructor of this point
when we create a constructor, when and instantiate an object, this is where to point it? Create a function

function main(){
    this.aad = 234;
    this.def = function(){
                console.log(this);
            };
    this.foo = function(){
            console.log(this === xxx);
        };
    this.xoo = function(){
            console.log(this === main);
        };
}
var xxx = new main();
xxx.def(); 
xxx.foo();
xxx.xoo();

clipboard.png

It can be seen xxx.def function or point to the main function, but this is only the context to print out, but when we print this constructor and the achievement of the object can be seen when comparing the difference, with the main constructor more out value it is false; shown to be true when compared to xxx function, so what this constructor of an object refers to the current instance.

This is what I learned about this summary, we hope to give those who need some help, then what's the lack of hope that the great God can point out.

Guess you like

Origin www.cnblogs.com/10manongit/p/12651276.html