call apply 的基本用法与区别
知识点:apply为应用的意思, 函数的arguments代指入参
1.基本应用
var a = function () {
console.log(this.dog)
};
var b = {};
b.dog = 'yyy'
a.apply(b) //输出了yyy
a.apply(b)意思为将a方法应用到b上;
基本用法上,call用法与apply基本一致
a.call(b) //输出了yyy
var a = function () {
this.dog = 'xxx';
console.log(this.dog)
};
var b = {};
b.dog = 'yyy'
a.apply(b) //输出了xxx (b的dog被修改成xxx了)
2.区别
var a = function () {
console.log(arguments);
};
var b = {};
var c = [1, 2];
a.call(b, c);
//
a.apply(b, c);
//
a.apply(b,[1,2]); a.call(b,[1,2])这两种写法都可以;但是apply会把数组参数当成N个入参传入,call会当成一个入参传入。
a.call(b,{1:1,2:2});这种写法也可以(call接收对象),但是apply只能接收数组
相关推荐
softwear 2020-08-21
ZGCdemo 2020-08-16
northwindx 2020-05-31
zrtlin 2020-11-09
xuebingnan 2020-11-05
wikiwater 2020-10-27
heheeheh 2020-10-19
Crazyshark 2020-09-15
jczwilliam 2020-08-16
littleFatty 2020-08-16
idning 2020-08-03
jinxiutong 2020-07-26
lanzhusiyu 2020-07-19
Skyline 2020-07-04
xiaofanguan 2020-06-25
Aveiox 2020-06-23