SB. 's proposal
Ask who can explain this value
var name = "the window";
var object = {
Name: "My Object",
Getname:function () {
return this.name;
}
};
Alert ((Object.getname = Object.getname) ());
--------the Window
After explanation
Object.getnamefunc () (); I understand that the window,
But (Object.getname = object.getname) () How is the window?
var a = 1;
A = a return what, you know why the window
The last example of a god
var name = "the window";
var object = {
Name: "My Object",
Getnamefunc:function () {
return this.name;
}
};
var a = Object.getnamefunc;
Alert (A ()); //the Window
Alert (Object.getnamefunc ()); My Object
Finally some God explained
Because a () is actually WINDOW.A ()
So when you execute a (), this points to the window
THIS.name is window.name.
Object.getname = Object.getname;
Alert (Object.getname ()); My Object
Alert ((Object.getname = Object.getname) ());//"The window"
Still don't understand why the first one is obj and the second one is window.
How do you understand this explanation?
Finally, he understood.
Some slag: I probably understand the implementation
Alert ((Object.getname = object.getname)); Return
function () {
return this.name;
}
This is window.
Yes, right.
Some slag: Yes.
That's it.
Some slag: just a pair () returns a function
(Object.getname = Object.getname) returned is actually the result of the = operation
A slag: Thank you.
satisfactorily resolved
Some God summaryWhen a function is called as a function rather than as a method, this refers to the global object
When a function is called as a method of an object, this points to that object
Object.getnamefunc (), at which point the Object.getnamefunc is a reference type with base object,this pointing to base, so return the value of Object.name
Name = Object.getnamefunc, test as the identifier, generates a value for the other reference type, when base has been reset from object to null, which will point to Global (window), so the value returned is window.name.
The key to the problem is that the intermediate value of the reference type (type Reference) has changed.
JavaScript about this (a question raised by a slag)