Javascript call和apply區(qū)別及使用方法
一、方法的定義
call方法:
語法:fun.call(thisArg[, arg1[, arg2[, ...]]])
定義:調(diào)用一個(gè)對象的一個(gè)方法,以另一個(gè)對象替換當(dāng)前對象。
說明:
call 方法可以用來代替另一個(gè)對象調(diào)用一個(gè)方法。call 方法可將一個(gè)函數(shù)的對象上下文從初始的上下文改變?yōu)橛?thisArg 指定的新對象。
如果沒有提供 thisArg參數(shù),那么 Global 對象被用作 thisArg。
apply方法:
語法:fun.apply(thisArg[, argsArray])
定義:應(yīng)用某一對象的一個(gè)方法,用另一個(gè)對象替換當(dāng)前對象。
說明:
如果 argArray 不是一個(gè)有效的數(shù)組或者不是 arguments 對象,那么將導(dǎo)致一個(gè) TypeError。
如果沒有提供 argArray 和 thisArg 任何一個(gè)參數(shù),那么 Global 對象將被用作 thisArg, 并且無法被傳遞任何參數(shù)。
二、兩者區(qū)別
兩個(gè)方法基本區(qū)別在于傳參不同
2.1、call方法:
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方法:
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);
三、作用實(shí)例
3.1、類的繼承
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(“設(shè)計(jì)蜂巢”,24,”男”);
test.alertName();//設(shè)計(jì)蜂巢
test.alertAge();//24
test.alertSex();//男
3.2、回調(diào)函數(shù)
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, ‘設(shè)計(jì)蜂巢', 2);
album.get_owner(function (owner) {
alert(‘The album' + this.name + ‘ belongs to ‘ + owner);
});
- javascript中apply和call方法的作用及區(qū)別說明
- js apply/call/caller/callee/bind使用方法與區(qū)別分析
- JavaScript學(xué)習(xí)點(diǎn)滴 call、apply的區(qū)別
- JavaScript中apply與call的用法意義及區(qū)別說明
- 在JavaScript中call()與apply()區(qū)別
- JavaScript中的this,call,apply使用及區(qū)別詳解
- 理解Javascript的caller,callee,call,apply區(qū)別
- 深入理解JavaScript中的call、apply、bind方法的區(qū)別
- javascript中apply、call和bind的使用區(qū)別
- 深入理解關(guān)于javascript中apply()和call()方法的區(qū)別
- JavaScript中的call和apply的用途以及區(qū)別
- JavaScript中call和apply方法的區(qū)別實(shí)例分析
相關(guān)文章
javascript中數(shù)組的多種定義方法和常用函數(shù)簡介
本文簡單介紹了javascript一維數(shù)組和二維數(shù)組的定義方法集錦以及常用函數(shù)簡介。2014-05-05javascript 進(jìn)階篇3 Ajax 、JSON、 Prototype介紹
javascript 進(jìn)階篇3 Ajax 、JSON、 Prototype介紹,學(xué)習(xí)js的朋友可以參考下2012-03-03Javascript獲取窗口(容器)的大小及位置參數(shù)列舉及簡要說明
Javascript獲取窗口(容器)的大小及位置一系列的東西比較多,容易混淆,在這里列舉及簡要說明下,需要的朋友可以參考下2012-12-12深入理解JavaScript系列(22):S.O.L.I.D五大原則之依賴倒置原則DIP詳解
這篇文章主要介紹了深入理解JavaScript系列(22):S.O.L.I.D五大原則之依賴倒置原則DIP詳解,本文講解了DIP and JavaScript、何時(shí)依賴注入等內(nèi)容,需要的朋友可以參考下2015-03-03JS嚴(yán)格模式知識(shí)點(diǎn)總結(jié)
本篇文章給大家整理了JS嚴(yán)格模式下的相關(guān)知識(shí)點(diǎn)以及代碼實(shí)例分享,感興趣的跟著小編一起學(xué)習(xí)下吧。2018-02-02詳細(xì)講解JS節(jié)點(diǎn)知識(shí)
最近發(fā)現(xiàn)DOMDocument對象很重要,還有XMLHTTP也很重要2010-01-01