因為最近接手了一個老項目,關于類的封裝很有趣,正好借機復習一下apply()和call()兩位好兄弟。
apply和call是Function對象原型鏈上的函數,每個function都自帶這兩個屬性。兩者的功能相同,調用方式略有差異,下面的例子主要用call作為例子演示,例子都來自MDN。
語法
func.apply(thisArg, [argsArray])
thisArg
可選參數,function
函數運行時使用的this
值。非嚴格模式下,則指定為null
或undefined
時會自動替換為指向全局對象,瀏覽器環境為window,node環境為global,在react項目中全局對象得打印看一下,有驚喜。
argsArray
可選參數,一個數組或者類數組對象,其中的數組元素將作為單獨的參數傳給 func 函數。
function.call(thisArg, arg1, arg2, ...)
thisArg
可選參數,function
函數運行時使用的this
值。非嚴格模式下,則指定為null
或undefined
時會自動替換為指向全局對象,瀏覽器環境為window,node環境為global,在react項目中全局對象得打印看一下,有驚喜。
arg1, arg2, ...
可選參數,指定的參數列表。
第一個例子,尋找一個數組中的最大值
// 方式一
const numbers = [5, 6, 2, 3, 7];
const max = Math.max.apply(null, numbers);
console.log(max); // 7
// 方式二,感謝es6
const numbers = [5, 6, 2, 3, 7];
const max = Math.max(...numbers);
console.log(max); // 7
第二個例子,用 apply 將數組添加到另一個數組
// 方式一
var array = ['a', 'b'];
var elements = [0, 1, 2];
array.push.apply(array, elements);
console.info(array); // ["a", "b", 0, 1, 2]
// 方式二,再次感謝es6
var array = ['a', 'b'];
var elements = [0, 1, 2];
array.push(...elements)
console.info(array); // ["a", "b", 0, 1, 2]
上述兩個例子都是體現了apply可以將數組或者類數組對象傳給func這一特性
第三個例子,使用 call 方法調用函數并且指定上下文的 this
function greet() {
var reply = [this.animal, 'typically sleep between', this.sleepDuration].join(' ');
console.log(reply);
}
var obj = {
animal: 'cats', sleepDuration: '12 and 16 hours'
};
greet.call(obj); // cats typically sleep between 12 and 16 hours
理解:greet運行時,this指向了obj,obj的屬性animal為cats,sleepDuration為12 and 16 hours,greet的數組reply中的this.animal這是指向
obj.animal,this.sleepDuration指向
obj.sleepDuration
這里我用的是指向,其實也不正確,call只是在兩個function建立的一層關聯,一個對象就可以通過委托訪問另一個對象的屬性和函數,就如我的標題所說,委托
,不過指向好像更便于理解。
第四個例子,使用 call 方法調用函數并且不指定第一個參數
var sData = 'Wisen';
function display() {
console.log('sData value is %s ', this.sData);
}
display.call(); // sData value is Wisen
這個例子,對應的是,非嚴格模式下,不指定第一個參數,這個參數會指向全局對象。
第五個例子,使用 call 方法調用父構造函數,也就是繼承
function Product(name, price) {
console.log(this)
this.name = name;
this.price = price;
}
function Food(name, price) {
Product.call(this, name, price);
this.category = 'food';
}
function Toy(name, price) {
Product.apply(this, [name, price]);
this.category = 'toy';
}
var pro = new Product('a', 7);
var cheese = new Food('feta', 5);
var fun = new Toy('robot', 40);
console.info(cheese.name); // feat
console.info(fun.name); // robot
補充知識點,new一個實例的過程中,發生了什么事情
(1) 創建一個新對象;
(2) 將構造函數的作用域賦給新對象(將構造函數的 this 指向了這個新對象);
(3) 執行構造函數中的代碼(為這個新對象添加屬性);
(4) 返回新對象。
let Parent = function (name, age) {
this.name = name;
this.age = age;
};
let newMethod = function (Parent, ...rest) {
var obj = {};
obj.__proto__ = Parent.prototype;
Parent.apply(obj, rest)
return obj;
};
const child = newMethod(Parent,'團子', '25');
console.log(child)
我們以第一個pro為例,打印this,發現this是Product,所以可以作以下分析,其實和上面是一個東西,沒差的
(1)以構造器的prototype屬性為原型,創建新對象c;var c = Object.create(Product.prototype)
(2)將c和Product上的屬性關聯,執行,參數實例化;Product.apply(c, [name, price]);
(3)返回c
// ES5構造函數
let Parent = function (name, age) {
this.name = name;
this.age = age;
};
let newMethod = function (Parent, ...rest) {
// 1.以構造器的prototype屬性為原型,創建新對象;
let c = Object.create(Parent.prototype);
// 2.將c和Product上的屬性關聯,執行
Parent.apply(c, rest);
// 3.返回第一步的對象
return c;
};
let newMethod = function (Parent, ...rest) {
var obj = {};
obj.__proto__ = Base.prototype;
Parent.apply(obj, rest)
return c;
};
const child = newMethod(Parent,'團子', 25);
console.log(child)
此時,我們再去理解Product.call(this, name, price);
Product獲取了name和price,之后this和Product相關聯,this就可以通過委托訪問product的屬性和函數,迷惑性的行為也可以被稱為繼承