Call () and apply () in JavaScript distinguish _javascript tips

Source: Internet
Author: User

If you haven't contacted a dynamic language, it will be a magical and weird feeling to understand JavaScript in a compiled language, because the things that are often impossible in consciousness happen, and even feel unreasonable. If you're learning JavaScript in a free and changing language, this is the way you feel. , then from now on, please put down your "prejudice", because this is absolutely a new continent for you, let Javascrip

Well, to start with, understand the Javascrtipt dynamic transformation Run-time context characteristics, which is mainly embodied in the application, call two methods of use.

Definition of a method

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 instead of another object. The call method can change the object context of a function from the initial context to the new object specified by Thisobj.
If the thisobj parameter is not supplied, the Global object is used as a thisobj.

Apply method:

Syntax: Apply (Thisobj,[argarray])

Definition: Applies one method of an object and 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 you do not supply any of the Argarray and thisobj parameters, the Global object will be used as a thisobj and cannot be passed any parameters.

--------------------------------------------------------------------------------

Note: The call and apply method are exactly the same, but apply is used in the way of the argument, and it is passed as an array.

code example:

function Animal (name) {
this.name = name;
This.showname = function () {
console.log (this.name);}
;
} function Cat (name) {
Animal.call (this, name);
}
Cat.prototype = new Animal ();
function Dog (name) {
animal.apply (this, name);
}
Dog.prototype = new Animal ();
var cat = new Cat ("Black Cat"); Call must be an object
var dog = new Dog (["Black Dog"]);//apply must be array
cat.showname ();
Dog.showname ();
Console.log (cat instanceof Animal);

-------------------------------------------------------------------------------

Simulate call, apply the This replacement

function Animal (name) {
this.name = name;
This.showname = function () {
alert (this.name);
}
;}; function Cat (name) {
this.superclass = Animal;
This.superclass (name);
Delete superclass;
}
var cat = new Cat ("Black Cat");
Cat.showname ();

Summarize:

Their respective definitions:

Apply: Use one of the methods of an object to replace the current object with another object.

Call: Invokes one of the object's methods to replace the current object with another object.

What they have in common:

can be used instead of another object to invoke a method that changes the object context of a function from the initial context to the new object specified by Thisobj. "--excerpted from JScript5.5. chm

The difference between them:

Apply: You can have up to two parameters-the new this object and an array Argarray. If you pass multiple arguments to the method, the arguments are written into the array, and of course, even if there is only one argument, it is written into the array. If Argarray is not a valid array or is not a arguments object, it will result in a typeerror. If you do not supply any of the Argarray and thisobj parameters, the Global object will be used as a thisobj and cannot be passed any parameters.

Call: is a direct parameter list, mainly used in the JS object when each method calls each other, so that the current instance pointer is consistent, or in special circumstances need to change the this pointer. If the thisobj parameter is not supplied, the Global object is used as a thisobj.

To put it simply, the apply and call functions are just a different form of the incoming argument list: Func.call (FUNC1,VAR1,VAR2,VAR3) corresponds to the Apply: Func.apply (FUNC1,[VAR1,VAR2, VAR3])

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.