-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
5.call apply bind 小结 #5
Labels
JavaScript
About JavaScript
Comments
你好,再this的那边,你讲的是apply讲this指向新对象,这边说的是call呢 |
call和apply都可以改变this的指向,call改变this指向传入的是字符串对象,而apply传入的是数组对象 |
所以在new那边,改变this指向,两个方法都是可以的吗 |
对是的,apply 和 call 的区别是 call 方法接受的是若干个参数列表,而 apply 接收的是一个包含多个参数的数组。本质上是改变this的指向,只不过区别在于传入的参数列表存在差别,如果传入的参数都不是数组,那么这两个方法是一样的效果。你可以尝试着用实例代码跑一遍。 |
噢噢,知道了,非常感谢 |
不客气😄加油🚀 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
apply()
apply() 方法调用一个函数, 其具有一个指定的this值,以及作为一个数组(或类似数组的对象)提供的参数
语法:
thisArg:在 fun 函数运行时指定的 this 值。需要注意的是,指定的 this 值并不一定是该函数执行时真正的 this 值,如果这个函数处于非严格模式下,则指定为 null 或 undefined 时会自动指向全局对象(浏览器中就是window对象),同时值为原始值(数字,字符串,布尔值)的 this 会指向该原始值的自动包装对象。
argsArray:一个数组或者类数组对象,其中的数组元素将作为单独的参数传给 fun 函数。如果该参数的值为null 或 undefined,则表示不需要传入任何参数。从ECMAScript 5 开始可以使用类数组对象。浏览器兼容性请参阅本文底部内容。
apply 和 call 的区别
其实 apply 和 call 基本类似,他们的区别只是传入的参数不同。
语法:
所以 apply 和 call 的区别是 call 方法接受的是若干个参数列表,而 apply 接收的是一个包含多个参数的数组。
bind 和 apply、call 区别
以上code我们会发现并没有输出.
bind()方法创建一个新的函数, 当被调用时,将其this关键字设置为提供的值,在调用新函数时,在任何提供之前提供一个给定的参数序列。
所以我们可以看出,bind 是创建一个新的函数,我们必须要手动去调用:
一个面试经典问题:new 的过程了
伪代码
所以我们可以看到,在 new 的过程中,我们是使用 call 改变了 this 的指向。
运用场景
总结 apply 、 call 、bind
三者都是用来改变函数的this对象的指向的;
apply 、 call 、bind 三者第一个参数都是this要指向的对象,也就是想指定的上下文;
apply 、 call 、bind 三者都可以利用后续参数传参;
bind 是返回对应函数,便于稍后调用;apply 、call 则是立即调用 。
参考网站
this、apply、call、bind
深入浅出 妙用Javascript中apply、call、bind
The text was updated successfully, but these errors were encountered: