xxholly32 / Blog

个人空间
https://www.xxholly32.com
4 stars 0 forks source link

apply的意思理解 #24

Open xxholly32 opened 5 years ago

xxholly32 commented 5 years ago

定义一个函数mul

function mul(a,b){
    return this+(a*b);
}

接着我们在控制台上打印出

console.log(mul.call(null,2,3));
console.log(mul.call('s',2,3));
console.log(mul.call(3,2,3));
console.log(mul.apply(null,[2,5]));
console.log(mul.apply(2,[2,5]));

分别为:

[object Window]6
s6
9
[object Window]10
12

function.apply(obj, arg)

function(arg) this = obj