JavaScript about this (a question raised by a slag)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.