Repository navigation
Description
apply 和 call 的区别
ECMAScript 规范给所有函数都定义了 call 与 apply 两个方法,它们的应用非常广泛,它们的作用也是一模一样,只是传参的形式有区别而已。
apply( )
apply 方法传入两个参数:一个是作为函数上下文的对象,另外一个是作为函数参数所组成的数组。
var obj = {
name : 'linxin'
}
function func(firstName, lastName){
console.log(firstName + ' ' + this.name + ' ' + lastName);
}
func.apply(obj, ['A', 'B']); // A linxin B可以看到,obj 是作为函数上下文的对象,函数 func 中 this 指向了 obj 这个对象。参数 A 和 B 是放在数组中传入 func 函数,分别对应 func 参数的列表元素。
call( )
call 方法第一个参数也是作为函数上下文的对象,但是后面传入的是一个参数列表,而不是单个数组。
var obj = {
name: 'linxin'
}
function func(firstName, lastName) {
console.log(firstName + ' ' + this.name + ' ' + lastName);
}
func.call(obj, 'C', 'D'); // C linxin D对比 apply 我们可以看到区别,C 和 D 是作为单独的参数传给 func 函数,而不是放到数组中。
对于什么时候该用什么方法,其实不用纠结。如果你的参数本来就存在一个数组中,那自然就用 apply,如果参数比较散乱相互之间没什么关联,就用 call。
apply 和 call 的用法
1.改变 this 指向
var obj = {
name: 'linxin'
}
function func() {
console.log(this.name);
}
func.call(obj); // linxin我们知道,call 方法的第一个参数是作为函数上下文的对象,这里把 obj 作为参数传给了 func,此时函数里的 this 便指向了 obj 对象。此处 func 函数里其实相当于
function func() {
console.log(obj.name);
}2.借用别的对象的方法
先看例子
var Person1 = function () {
this.name = 'linxin';
}
var Person2 = function () {
this.getname = function () {
console.log(this.name);
}
Person1.call(this);
}
var person = new Person2();
person.getname(); // linxin从上面我们看到,Person2 实例化出来的对象 person 通过 getname 方法拿到了 Person1 中的 name。因为在 Person2 中,Person1.call(this) 的作用就是使用 Person1 对象代替 this 对象,那么 Person2 就有了 Person1 中的所有属性和方法了,相当于 Person2 继承了 Person1 的属性和方法。
3.调用函数
apply、call 方法都会使函数立即执行,因此它们也可以用来调用函数。
function func() {
console.log('linxin');
}
func.call(); // linxincall 和 bind 的区别
在 EcmaScript5 中扩展了叫 bind 的方法,在低版本的 IE 中不兼容。它和 call 很相似,接受的参数有两部分,第一个参数是是作为函数上下文的对象,第二部分参数是个列表,可以接受多个参数。
它们之间的区别有以下两点。
1.bind 发返回值是函数
var obj = {
name: 'linxin'
}
function func() {
console.log(this.name);
}
var func1 = func.bind(obj);
func1(); // linxinbind 方法不会立即执行,而是返回一个改变了上下文 this 后的函数。而原函数 func 中的 this 并没有被改变,依旧指向全局对象 window。
2.参数的使用
function func(a, b, c) {
console.log(a, b, c);
}
var func1 = func.bind(null,'linxin');
func('A', 'B', 'C'); // A B C
func1('A', 'B', 'C'); // linxin A B
func1('B', 'C'); // linxin B C
func.call(null, 'linxin'); // linxin undefined undefinedcall 是把第二个及以后的参数作为 func 方法的实参传进去,而 func1 方法的实参实则是在 bind 中参数的基础上再往后排。
在低版本浏览器没有 bind 方法,我们也可以自己实现一个。
if (!Function.prototype.bind) {
Function.prototype.bind = function () {
var self = this, // 保存原函数
context = [].shift.call(arguments), // 保存需要绑定的this上下文
args = [].slice.call(arguments); // 剩余的参数转为数组
return function () { // 返回一个新函数
self.apply(context,[].concat.call(args, [].slice.call(arguments)));
}
}
}
Activity
DummyLIU commentedon Nov 20, 2017
good
Kingwantfly commentedon Jan 8, 2018
完美,讲的很好
xsdream commentedon Jan 18, 2018
nice!
Fang0408 commentedon Feb 25, 2018
ES6里面的call还能用解构来传参
const f = function (a, b, c) { console.log(a, b, c) } const arr = [1, 2, 3] f.call(null, ...arr)laclys commentedon Mar 15, 2018
@Fang0408 直接f(...arr)不就得了
124design commentedon Apr 28, 2018
nice!
fonice commentedon Apr 30, 2018
2.借用别的对象的方法
我的理解是 Person1 的 this 变成 Person2的this,并执行一遍Person1函数
Person2就多了一个name属性
ettingshausen commentedon May 19, 2018
补充一个使用
apply的例子。比如求一个数组的最大值,使用
Math.max函数,但是这个函数入参不支持数组,只能是将多个参数逐个传入,用逗号分隔。有了
apply,就可以这么调用:HiIcy commentedon Jun 9, 2018
nice!
Rudy24 commentedon Jun 26, 2018
@lin-xin 楼主这里有点问题吧
Person1.call(this) 的作用就是使用 Person1 对象代替 this 对象,Person1.call(this), 按照理解应该是 把Person1函数中的this指向了Person2中的this
ycshill commentedon Aug 10, 2018
var Person1 = function () {
this.name = 'linxin';
}
var Person2 = function () {
this.getname = function () {
console.log(this.name);
}
Person1.call(this);
}
var person = new Person2();
person.getname();
这个例子不是太理解,各位楼主有没有详细解答的
web-display commentedon Aug 28, 2018
在一定程度上Person1.call(this) 的作用就是使用 Person1 对象代替 this 对象是成立的,那样子的话Person2就不能拥有自己的属性了,我的理解是Person1.call(this)执行完毕后讲Person1的属性复制到了Person2中
ZhangzyGY commentedon Sep 28, 2018
nice!
16slowly commentedon Oct 13, 2018
以上返回的函数体中的 self.apply(context,[].concat.call(args, [].slice.call(arguments))) 里第二个参数为啥是
[].concat.call(args, [].slice.call(arguments)), 它不就应该是 args 吗 ?@lin-xin
MontageD commentedon Nov 8, 2018
好文,赞
montilloo commentedon Dec 1, 2018
Perfect!
ChrisSong1994 commentedon Dec 10, 2018
返回的是一个函数,可以继续添加参数
devindo commentedon Jan 25, 2019
Math.max(...[1,2,3])
zqzq3 commentedon Apr 17, 2019
完全看不懂
leiyangs commentedon Jun 14, 2019
豁然开朗
ximelly commentedon Jun 20, 2019
同问
izayoi-hibiki commentedon Jun 20, 2019
输出:
从输出可以看出
args是f.binding里除了第一个参数剩下所有的参数,而[].slice.call(arguments)是f.binding后第二个括号里的参数,args的 this 是Function.prototype.binding,[].slice.call(arguments)的 this 是f.binding(obj)()。ximelly commentedon Jun 20, 2019
明白了 多谢!
httol commentedon Aug 8, 2019
这个是标准的闭包写法,函数嵌套函数的时候,被嵌套的函数的作用域链是单独的,而且每次执行外部函数时,这个作用域链总是会生成新的链,像this和arguments都不会和外部函数共享,也是单独的,比如嵌套函数想拿到外部的arguments和this需要在嵌套函数外面用变量做存储才可以,类似let self = this,let args = arguments。例子中嵌套函数的arguments就是自己函数的,而不是外部函数的。
1793523411 commentedon Jul 6, 2020
懂了,真棒!!!
haohao3308 commentedon Jun 1, 2021
SolitaryBirdDeadWood commentedon Jun 29, 2022
还有添加
SolitaryBirdDeadWood commentedon Jun 29, 2022