The this pointer in the "Go" javascript

Source: Internet
Author: User

case One: The parameter passed in is the alias of the function, then this is to point to window ;

Scenario Two: The incoming parameter is the new over the constructor, then this is to point to the instantiated object itself;

Scenario Three: If we want to put an incoming function object inside this pointer to the object defined by the external literal, then we are using the Apply and Call

We can see my conclusion in code as follows:

<script type= "Text/javascript" >varName = "I am Window";varobj ={name:"Sharpxiajun", Job:"Software", Ftn01:function(obj) {obj.show (); }, Ftn02:function(FTN) {ftn (); }, Ftn03:function(FTN) {Ftn.call ( This);//call and apply is to point the this pointer to the first parameter of the method    }};functionPerson (name) { This. Name =name;  This. Show =function() {Console.log ("Name:" + This. Name); Console.log ( This); }}varp =NewPerson (' person '); Obj.ftn01 (P); Obj.ftn02 (function() {Console.log ( This. Name); Console.log ( This; }); o Bj.ftn03 (function() {Console.log ( This. Name); Console.log ( This);});</script>

The results are as follows:

Finally, let's summarize:

if the JavaScript The language didn't pass new. (including object literal definition), call and Apply change the This of the function pointer, function of this The pointers are all pointing to the window the .

The this pointer in the "Go" javascript

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.