Call and Apply differences

Source: Internet
Author: User

call and apply are basically a difference in the meaning that the second argument to the arguments can be any type, and the second argument to apply must be an array or a single-byte.
Call Method:

Syntax: Call (Thisobj,object)
Definition: Invokes one method of an object, replacing the current object with another object.
Description
The call method can be used to invoke a method in place of another object. The call method can change the object context of a function from the initial context to a new object specified by Thisobj.
If the Thisobj parameter is not provided, then the Global object is used as the thisobj.
/Create a Class A
function A () {
The following code is run when the class is instantiated
The execution context object at this point is this, which is the current instance object
This.message = "message of a";
This.getmessage = function () {
return this.message;
}
}
Create a Class A instance object
var a = new A ();
Call the class instance GetMessage method to get the message value
Alert (A.getmessage ());
Create a Class B
function B () {
This.message = "message of B";
This.setmessage = function (msg) {
This.message = msg;
}
}
Create a Class B instance object
var a = new B ();
The Setmessage method of dynamically assigning B to object A, note that a does not have this method in itself!
B.setmessage.call (A, "a message");
The message "A" is displayed below
Alert (A.getmessage ());
The GetMessage method of dynamically assigning a to object B, note that B itself also does not have this method!


Apply method:
Syntax: Apply (Thisobj,[argarray])
Definition: A method of applying an object that replaces the current object with another object.
Description
If Argarray is not a valid array or is not a arguments object, it will result in a TypeError.
If none of the Argarray and Thisobj parameters are provided, then the Global object is used as a thisobj and cannot be passed any parameters.

function print (A, B, C, D) {
Alert (A + B + C + D);
}
Function Example (A, B, C, D) {
Using the call method to borrow print, parameter explicit break-through transfer
Print.call (This, a, B, C, D);
Use the Apply method to borrow print, the parameter is passed as an array,
Here directly with the JavaScript method within the arguments array itself
Print.apply (this, arguments);
or encapsulate an array of
Print.apply (This, [A, B, C, d]);
}
The "backlight script" is shown below
Example ("Back", "light", "foot", "Ben");

Cases
Print.call (window, "Back", "light", "foot", "Ben");
The foo parameter may be multiple
function foo () {
print.apply (window, arguments);
}

Call and Apply differences

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.