You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Function.prototype._bind=function(){if(typeofthis!=='function'){thrownewError('Function.prototype.bind - what is trying to be bound is not callable')}letself=this// 需要传入self的参数letargs=Array.prototype.slice.call(arguments,1)returnfunction(){returnself.apply(self,args)}}Function.prototype._bind=Function.prototype._bind||function(){}
call() 方法
call() 方法调用一个函数。简单理解为调用函数的方式,但是它可以改变函数的 this 指向
语法:fun.call(thisArg, arg1, arg2, ...)
参数:
thisArg:在 fun 函数运行时指定的 this 值
arg1,arg2:传递的其他参数
注意:
返回值就是函数的返回值,因为它就是调用函数
因此当我们想改变 this 指向,同时想调用这个函数的时候,可以使用 call,比如继承
call()无参数 / call(null) / call(undefined);这三种 this 都指向 window
实现 call 方法
Function.prototype._call=function(){if(typeofthis!=='function'){thrownewError('Function.prototype.bind - what is trying to be bound is not callable')}letself=thisletpointTo=arguments[0]||window// 需要传入self的参数letargs=Array.from(arguments).slice(1)pointTo.fn=selfpointTo.fn(...args)// 需要传入self的参数}Function.prototype._call=Function.prototype._call||function(){}
apply 方法
apply() 方法调用一个函数。简单理解为调用函数的方式,但是它可以改变函数的 this 指向,参数为数组形式
语法:fun.apply(thisArg, [arg1, arg2, ...])
参数:
thisArg:在 fun 函数运行时指定的 this 值
arg1,arg2:传递的其他参数放在中括号内
注意:
因此 apply 主要跟数组有关系,比如使用 Math.max() 求数组的最大值
和 call()的区别就是,apply()的参数放在中括号内 实现 call 方法
Function.prototype._apply=function(){if(typeofthis!=='function'){thrownewError('Function.prototype.bind - what is trying to be bound is not callable')}letself=thisletpointTo=arguments[0]||window// 需要传入self的参数let[args]=Array.from(arguments).slice(1)pointTo.fn=selfpointTo.fn(...args)// 需要传入self的参数}Function.prototype._apply=Function.prototype._apply||function(){}
Function.prototype.ownBind=function(context){let_self=this,args=[].slice.call(arguments,1)//ES5写法letfn=function(){return_self.apply(thisinstanceoffn ? this : context,args.concat([].slice,call(arguments)));}letfn1=newFunction();fn1.prototype=_self.prototype;fn.prototype=newfn1();returnfn;}
Activity
Amour1688 commentedon May 12, 2019
call
和apply
都是为了解决改变this
的指向。作用都是相同的,只是传参的方式不同。除了第一个参数外,
call
可以接收一个参数列表,apply
只接受一个参数数组。bind
绑定完之后返回一个新的函数,不执行。AnsonZnl commentedon May 12, 2019
上面同学说的太好了,简洁好记!!忍不住重复一遍。
myprelude commentedon Jun 13, 2019
bind(context,arguments) 返回一个函数
call(context,arg1,arg2...) 指定作用域 同时执行函数
apply(context,args) 指定作用域 同时执行函数,后面的参数是数组
实现bind
Damon99999 commentedon Jun 20, 2019
1:call与apply基本一样,唯一不一样的是接收的参数
2:bind则是改变this后返回一个新函数
Vi-jay commentedon Jul 29, 2019
Konata9 commentedon Aug 12, 2019
bind
,call
,apply
三者都可以改变this
的指向。其中
call
和apply
为立即执行,两者效果等价,只有在传參形式上有所区别。call
需要把参数一个一个传入fun.call(obj, arg1, arg2, arg3,...)
而apply
接受一个数组作为参数fun.apply(obj, [arg1, arg2, arg3, ...])
。bind
则是延时执行。const fb = fun.bind(obj) fb(arg1, arg2, ...)
在使用bind
之后,只会返回一个修改了作用域的函数,等再次调用时才会执行。jiamianmao commentedon Aug 14, 2019
BYC04 commentedon Sep 20, 2019
https://www.runoob.com/w3cnote/js-call-apply-bind.html
这个帖子对于三者区别写的很详细
rennzhang commentedon Apr 1, 2020
bind
bind() 方法不会调用函数,他会绑定其他元素,当其他元素触发事件时改变 this 的指向
语法:
fun.bind(thisArg, arg1, arg2, ...)
参数:
注意:
实现 bind 方法
call() 方法
call() 方法调用一个函数。简单理解为调用函数的方式,但是它可以改变函数的 this 指向
语法:
fun.call(thisArg, arg1, arg2, ...)
参数:
注意:
实现 call 方法
apply 方法
apply() 方法调用一个函数。简单理解为调用函数的方式,但是它可以改变函数的 this 指向,参数为数组形式
语法:
fun.apply(thisArg, [arg1, arg2, ...])
参数:
注意:
实现 call 方法
有错请指出,感谢
adi0754 commentedon Jun 9, 2020
blueRoach commentedon Jun 22, 2020
call、apply和bind作用都是为了改变this的指向
call、apply会立即执行此函数,两者等价,第一个参数一致。apply第二个参数是数据,call是接受一个参数列表
bind是返回一个新的函数。第二个参数也是一个参数列表
laboonly commentedon Aug 24, 2020
smile-2008 commentedon Sep 27, 2020
call() 和 apply() 作用都是改变 this 的指向,区别是传参的方式不同。除了第一个参数外,call() 可以接收一个参数列表,apply() 只接受一个参数数组。 bind() 绑定完之后返回一个新的函数,不执行。
bozaigao commentedon Oct 6, 2020
m7yue commentedon Oct 10, 2020
zxcdsaqwe123 commentedon Oct 15, 2021
call 改变this的指向,参数只有对象一个
apply 也能改变this的指向,让变量能调用别人的方法,可以传入参数,用数组传入参数
github-cxtan commentedon Feb 19, 2022
改变函数执行时的上下文,再具体一点就是改变函数运行时的this指向。
bind
bind方法是事先把fn的this改变为我们要想要的结果,并且把对应的参数值准备好,以后要用到了,直接的执行即可,也就是说bind同样可以改变this的指向,但和apply、call不同就是不会马上的执行
注意:bind这个方法在IE6~8下不兼容。
区别
上面看起来三个函数的作用差不多,干的事几乎是一样的,那为什么要存在3个家伙呢,留一个不就可以。所以其实他们干的事从本质上讲都是一样的动态的改变this上下文,但是多少还是有一些差别的..
call、apply与bind的差别
call和apply改变了函数的this上下文后便执行该函数,而bind则是返回改变了上下文后的一个函数。
call、apply的区别
他们俩之间的差别在于参数的区别,call和apply的第一个参数都是要改变上下文的对象,而call从第二个参数开始以参数列表的形式展现,apply则是把除了改变上下文对象的参数放在一个数组里面作为它的第二个参数。
yxllovewq commentedon Mar 9, 2022
call、apply、bind都是Function.protoype伤的方法,可以改变方法的this指向。
call和apply都是立即调用,区别为call是传入参数列表,apply是传入参数数组。
bind和call、apply的区别是bind会返回一个函数,不会立即调用。
function myCall (_this, ...args) {
_this.fn = this;
let result = _this.fn(...args);
delete _this.fn;
return result;
}
function myApply (_this, args) {
_this.fn = this;
let result = _this.fn(...args);
delete _this.fn;
return result;
}
function myBind (_this, args) {
return () => {
return myApply(_this, args);
}
}
xiaoqiangz commentedon May 29, 2022
Sobrium commentedon Sep 15, 2022
call、apply、bind都是用来改变this指向的,区别是,call和apply的参数不一样,第一个参数都是this指向要指向谁,而apply的第二个参数是一个数组,call则是参数列表,它们都是调用后就执行,bind则是调用后返回一个新的函数,bind的参数与call一样;
shengjie9 commentedon Nov 10, 2022
call和apply,bind函数是改变this指向,call和apply传入参数的方式不同,apply传参是以一个数组形式,call是直接在后面进行传参,call,apply和bind的区别是前者会直接调用函数,后者返回一个函数,函数的this指向已被改为bind传入的新this
call实现
`Function.prototype.mycall = function(thisArg,...args){
var fn = this
thisArg = thisArg ? Object(thisArg) : window
thisArg.fn = fn
var result = thisArg.fn(...args)
delete thisArg.fn
return result
}`
never123450 commentedon Sep 4, 2023
bind
、call
和apply
都是用于改变函数的执行上下文(即this
的指向)的方法,但它们的使用方式和效果略有不同。区别如下:
bind
方法:bind
方法会创建一个新的函数,并将指定的上下文对象绑定到该函数。bind
方法不会立即执行函数,而是返回一个绑定了上下文的新函数。bind
方法,这些参数会作为新函数的预设参数。call
方法:call
方法立即执行函数,并将指定的上下文对象绑定到该函数。call
方法接受一个上下文对象作为第一个参数,后续参数是传递给函数的参数列表。call
方法会改变函数的执行上下文并立即执行函数,返回函数的执行结果。apply
方法:apply
方法立即执行函数,并将指定的上下文对象绑定到该函数。apply
方法接受一个上下文对象作为第一个参数,第二个参数是一个数组,包含传递给函数的参数列表。apply
方法会改变函数的执行上下文并立即执行函数,返回函数的执行结果。下面是一个手写实现的
bind
方法的示例:上面的代码中,
myBind
函数接受一个函数func
、一个上下文对象context
和任意数量的预设参数args
。它返回一个新函数,该函数在执行时将绑定了上下文和预设参数,并将传递给新函数的参数与预设参数合并后,通过apply
方法调用原始函数。这样,我们就可以使用
myBind
方法来手动实现bind
的功能。panpanxuebing commentedon Dec 10, 2024
三者都可以改变函数 this 指向。
call 和 apply 作用相同,区别是除第一个参数外 call 接受一个参数列表,apply 接收一个数组。
bind 不会执行,返回一个新的函数。