Javascript Call and apply differences and how to use _ Basics

Source: Internet
Author: User

Definition of a method
Call Method:
Grammar: Fun.call (thisarg[, arg1[, arg2[, ...)]]
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 Thisarg.
If the Thisarg parameter is not supplied, the Global object is used as a thisarg.

Apply method:
Syntax: fun.apply (thisarg[, Argsarray])
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 Thisarg parameters, the Global object will be used as a thisarg and cannot be passed any parameters.

Two, the difference between the two
The basic difference between two methods is that the parameters are different
2.1. Call Method:

Copy Code code as follows:

function Product (name, price) {
THIS.name = name;
This.price = Price;
if (Price < 0)
Throw Rangeerror (' cannot create product ' + name + ' "with a negative price ');
return this;
}

function Food (name, price) {
Product.call (this, name, price);
this.category = ' food ';
}
Food.prototype = new Product ();

function Toy (name, price) {
Product.call (this, name, price);
This.category = ' toy ';
}
Toy.prototype = new Product ();

var cheese = new Food (' feta ', 5);
var fun = new Toy (' robot ', 40);


2.2. Apply method:
Copy Code code as follows:

function Product (name, price) {
THIS.name = name;
This.price = Price;
if (Price < 0)
Throw Rangeerror (' cannot create product ' + name + ' "with a negative price ');
return this;
}

function Food (name, price) {
Product.apply (this, arguments);
this.category = ' food ';
}
Food.prototype = new Product ();

function Toy (name, price) {
Product.apply (this, arguments);
This.category = ' toy ';
}
Toy.prototype = new Product ();

var cheese = new Food (' feta ', 5);
var fun = new Toy (' robot ', 40);


Iii. Examples of action

3.1. Inheritance of class

Copy Code code as follows:

function Person (name,age) {
THIS.name = name;
This.age=age;
This.alertname = function () {
alert (this.name);
}
This.alertage = function () {
alert (this.age);
}
}

function Webdever (name,age,sex) {
Person.call (This,name,age);
This.sex=sex;
This.alertsex = function () {
alert (this.sex);
}
}

var test= new Webdever ("Design honeycomb", 24, "male");
Test.alertname ()//Design Hive
Test.alertage ();//24
Test.alertsex ()///Male


3.2. Callback function
Copy Code code as follows:

function Album (ID, title, owner_id) {
This.id = ID;
THIS.name = title;
this.owner_id = owner_id;
};
Album.prototype.get_owner = function (callback) {
var self = this;
$.get ('/owners/' + this.owner_id, function (data) {
Callback && Callback.call (self, data.name);
});
};
var album = new album (1, ' Design Hive ', 2);
Album.get_owner (function (owner) {
Alert (' the album ' + THIS.name + ' belongs to ' + owner);
});

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.