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);
});