The this in the function inside the function is pointing to the window

Source: Internet
Author: User

Just saw a question about this,

1 var name= "the window"; 2     var object={3        name: "Silence",4        packname:function () { 5             return this.name; 6         }7    }8     Console.log (Object.packname ());   

In general, in JavaScript, this points to the current object when the function executes . This refers to object. Output is silence

Change it.

1 var name= "the window"; 2     var object={3        name: "Silence",4        packname:function () { 5             return this.name; 6         }7    }8    var show=object.packname; 9     Console.log (Show ());

When there is no explicit execution of the current object, this points to the Global object window.

Here Show=object.packname;show (); Show does not explicitly indicate the execution object, so it is equivalent to Window.show (); The result is the window

Change it again.

1     var name= "the window";2 var silence={3 name: "Silence",4 packname:function () {5 return this.name;6         }7     }8 var seraph={9 Name: "Seraph",Ten packname:function () { One var fun=silence.packname; A return Fun (); -         } -     } theConsole.log (Seraph.packname ());

Like 2, fun (); get the Window,fun () to see the function inside the function pointing to the window. That's what I understand.

The this in the function inside the function is pointing to the window

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.