Call and apply basic usage tell you

Source: Internet
Author: User

JS usage also has a very good but some abstract usage--call and apply, today I want to summarize briefly:

var ob1={  //Object OB1            Name: "Ob1",            func1:function (a,b,c) {                alert (this.name+a+b+c);            }            }         var ob2={//Object OB2            Name: "Ob2",            func2:function () {                alert (this.name);            }            }          Ob1.func1.call (OB2, "1", "2", "3");  Ob1123,call Let object B call the func1 of a, pointing to a pointer to B, so that B can be a func1 to access to their own properties          Ob2.func2.call (OB1);  Ob1          ob1.func1.apply (ob2,[1,2,3]);//ob2123          ob2.func2.apply (OB1);//ob1

"Ob2.func2.call (OB1)" is equivalent to "OB1.FUNC2 ()", in fact Ob1 does not have fun2, but call can help him to borrow from the OB2 and the pointer points to the OB1,FUNC2 in this point to its caller ob1, The Name property of the output ob1. Apply is the same as call usage, but the arguments are passed in different ways.

You may feel a bit around, wondering where the convenience of this writing is. Let's briefly summarize when it should be used, when object 2 wants to invoke the method in object 1, so that when writing less code, Object 1 typically has similar properties to Object 2, similar in type or identical.

Call and apply basic usage tell you

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.