JavaScript Topic (SCOPE)

Source: Internet
Author: User

var length = 10function fn () {    alert (this.length)}var obj = {    Length:5,    method:function (FN) {        fn ()//? 
   arguments[0] ()//?}    } Obj.method (FN)

This is a topic that is seen in the group today, a lot of people to answer, but everyone at the beginning of the answer is wrong, what the answer is.

Let me start by announcing the correct answer: 10,1

Then I'll explain why:

First, a var length=10 is defined; it's nothing to say.

Then declare a function functions fn () {alert (this) alert (this.length)}

In this step you execute FN () will pop up 10, because length is defined under window, FN is also lowered in window, this point to window;

Then you define an object: var obj = {length:5, method:function (FN) {fn ()///? Arguments[0] ()//?}}

The object also has a length property and a method:

Finally, the method of methods under the object is called;

First run function (FN) {fn ()//? We see that FN is passed in as a parameter and then executes FN (), then when you execute FN (), it is the outside FN that executes, and the FN outside is under window, this is pointing to window, and he can only find the Length property under window. The length under obj cannot be found

Then run function (FN) {arguments[0] ()//?} We see that FN is passed in as a parameter, and then this parameter is executed, and this is the parameter, so the length of the parameter is 1, and the result is 1.

If you add a parameter Obj.method (FN, "Heihei") then the second result is 2,

If you don't quite understand that, then in the FN function, alert (this), maybe you'll understand.

The above for personal understanding, if there is a mistake or error forget to leave a message.

JavaScript Topic (SCOPE)

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.