在 Javascript 中是否有等效于 .apply 的不改变 this 的值? [英] In Javascript is there equivalent to .apply that doesn't change the value of this?
本文介绍了在 Javascript 中是否有等效于 .apply 的不改变 this 的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
看起来很简单,我想用参数数组调用一个函数.当然,我可以说 func.apply(this, ['some', 'arguments']);
但这会改变 func<中
this
的值/代码>.知道如何在不改变它的情况下做到这一点吗?
Seems easy enough, i want to call a function with array of arguments. Sure, i can say func.apply(this, ['some', 'arguments']);
but that will change the value of this
inside func
. Any idea how to do this without changing it?
推荐答案
注意,在 ES6 中你也可以这样写:
Note that in ES6 you can also write:
func(...someArray)
这里this
变成了func
里面的Window
,没什么用.但是如果你写:
Here this
becomes Window
inside func
, which is of no use. But if you write:
obj.func(...someArray)
this
在 func
里面会是 obj
.
这可能是我 5 年前寻找的功能,但那是 5 年前,所以谁记得:)
This may be the feature i was looking for 5 years ago, but that was 5 years ago, so who remembers :)
这篇关于在 Javascript 中是否有等效于 .apply 的不改变 this 的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文