Javascript call和apply區(qū)別及使用方法
一、方法的定義
call方法:
語法:fun.call(thisArg[, arg1[, arg2[, ...]]])
定義:調(diào)用一個(gè)對(duì)象的一個(gè)方法,以另一個(gè)對(duì)象替換當(dāng)前對(duì)象。
說明:
call 方法可以用來代替另一個(gè)對(duì)象調(diào)用一個(gè)方法。call 方法可將一個(gè)函數(shù)的對(duì)象上下文從初始的上下文改變?yōu)橛?thisArg 指定的新對(duì)象。
如果沒有提供 thisArg參數(shù),那么 Global 對(duì)象被用作 thisArg。
apply方法:
語法:fun.apply(thisArg[, argsArray])
定義:應(yīng)用某一對(duì)象的一個(gè)方法,用另一個(gè)對(duì)象替換當(dāng)前對(duì)象。
說明:
如果 argArray 不是一個(gè)有效的數(shù)組或者不是 arguments 對(duì)象,那么將導(dǎo)致一個(gè) TypeError。
如果沒有提供 argArray 和 thisArg 任何一個(gè)參數(shù),那么 Global 對(duì)象將被用作 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)文章
對(duì)JavaScript客戶端應(yīng)用編程的一些建議
這篇文章主要介紹了對(duì)JavaScript客戶端應(yīng)用編程的一些建議,主要針對(duì)MVC框架框架的一些相關(guān)使用問題,需要的朋友可以參考下2015-06-06
微信小程序自定義數(shù)據(jù)實(shí)現(xiàn)級(jí)聯(lián)省市區(qū)組件功能
這篇文章主要介紹了微信小程序自定義數(shù)據(jù)實(shí)現(xiàn)級(jí)聯(lián)省市區(qū)組件功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-03-03
深入理解JavaScript系列(43):設(shè)計(jì)模式之狀態(tài)模式詳解
這篇文章主要介紹了深入理解JavaScript系列(43):設(shè)計(jì)模式之狀態(tài)模式詳解,狀態(tài)模式(State)允許一個(gè)對(duì)象在其內(nèi)部狀態(tài)改變的時(shí)候改變它的行為,對(duì)象看起來似乎修改了它的類,需要的朋友可以參考下2015-03-03
JavaScript高級(jí)程序設(shè)計(jì)(第3版)學(xué)習(xí)筆記13 ECMAScript5新特性
通常而言,JavaScript由ECMAScript核心、BOM和DOM三部分構(gòu)成,前面的文章將ECMAScript核心部分粗略的過了一篇2012-10-10
淺談JavaScript函數(shù)的四種存在形態(tài)
下面小編就為大家?guī)硪黄獪\談JavaScript函數(shù)的四種存在形態(tài)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06

