本文介绍了在JavaScript中有没有等同于.apply不会改变这个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来很简单,我想用参数数组调用一个函数。当然,我可以说 func.apply(this,['some','arguments']); 但会改变 this func 内。任何想法如何做到这一点而不改变它?

解决方案

请注意,在ES6中,您还可以编写:

  func(... someArray)


$ b $在 func 中,这个变成 Window ,这是没用的。但如果你写:

  obj.func(... someArray)

func 内的 code> obj



这可能是我5年前寻找的功能,但那是5年前的,所以谁记得:)

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?

解决方案

Note that in ES6 you can also write:

func(...someArray)

Here this becomes Window inside func, which is of no use. But if you write:

obj.func(...someArray)

this in inside func will be obj.

This may be the feature i was looking for 5 years ago, but that was 5 years ago, so who remembers :)

这篇关于在JavaScript中有没有等同于.apply不会改变这个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 05:45