JavaScript First (vii)----------this pointing problem

Source: Internet
Author: User

Implement chained invocation pattern by imitating jquery

Sometimes we need to call the method in a row like jquery, and this is what we're going to use.

  

var Deng = {    smoke:function () {         console.log ("smoke,cool!!");        return this;    },    firehair:function () {        console.log (' Fire ');        return this;    },    drink:function () {         console.log ("Drink");        return this;    }} Deng.smoke (). Firehair (). drink ();

The above code is the implementation of the chain call, when the function call execution will return to themselves.

This points to the problem

1. Function precompilation process, this----->window

2. Global scope this-----> window

3.call/apply, change this point, point to the first parameter

4.object.function () {} This------> object

For example:

var obj = {age:20,smoke:function () {console.log (this);}} var smoke1 = Obj.smoke;smoke1 ();

  

  in this example, the function body to smoke1, this time smoke1 execution, it is equivalent to their own execution, no one tune use it, so this time, the normal go pre-compilation link, pointing to the window.

One more complication, for example:

              var name = "222";              var a = {                  name: "111",                  say:function () {                      console.log (this.name);                  }              }              var fun = A.say;              Fun ();  222              A.say ();  111              var b = {                  name: "333",                  say:function (fun) {fun                      ();                  }              }              B.say (A.say); 222              B.say = A.say;               B.say (); 333

  

first, the object A's function body to the fun, and then fun execution is the equivalent of executing its own function, this is the window, and then object A calls its own say method, this point to a, then the third is the A object inside the function body

Take out their own execution, this time this point is the window, and finally the A.say inside the function body to the B.say function body, this time this point to B, with the object a inside the method.

how to represent a property

  We generally use the Obj.prop method to refer to attributes, but sometimes when we need to call a property via a variable parameter, we will get an error, so we use the following form

Console.log (obj["prop");//obj["prop"]

  

  This method calls the property, which is the final form of the system, even if we use the form of a point to make a call to the property, the system will eventually convert it into the form of brackets. The other is to call the genus in the form of brackets

and the ability to splice attributes:

           function Getprop (num) {                var obj = {                    friend1: "A",                    friend2: "B",                    Friend3: "C"                }                Console.log (obj ["Friend1"]+num+obj["Friend2"]+obj["Friend3"]);            }            Getprop ("E");//AEBC

JavaScript First (vii)----------this pointing problem

Related Article

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.