Understanding of the call () method in JavaScript (and comparing the Apply method)

Source: Internet
Author: User

Call Usefulness: Assign all properties and methods of B to target object A dynamically, noting that a does not have these properties and methods in itself!

The call syntax is: B.call (THISOBJ,ARG1,ARG2,ARG3)

Explanation: Thisobj is the target object (the target object to which the method property is assigned), ARG1,ARG2,ARG3 is a parameter, can be one, or it can be multiple

Scenario One: Only the first parameter of call (that is, the target object) is preserved

varb =function() {     This. Name = "123";  This. Message =function(obj) {console.log (obj);    };  This. ShowName =function(obj) {console.log (obj); };};varA = {};//define an empty object aB.call (a);//assign all properties and methods of B to object a dynamically, and note that a does not have these properties and methods in itself!A.message ("Displays the message specified by a");//displays the message specified by aA.showname ("Show information specified by a");//Displays the show information specified by aConsole.log (A.name);//123

Understanding of the call () method in JavaScript (and comparing the Apply method)

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.