在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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你不能,因为
this
在 JavaScript 中的工作方式。继续阅读:查看“输入执行上下文” 部分ECMAScript 规范:当您调用函数时,
this
的值由其左侧的内容(称为激活对象)决定。让我们创建一个名为 steve 的函数,并将他放入一个对象中:...当我们将 steve 称为 obj.method() 时,他的
this
是obj
,因为obj
是激活对象。棘手的情况是当函数调用的左侧没有任何内容时:
函数调用的左侧没有任何内容 — 实际上,它是 null - 因此
this
的值被设置为全局对象的默认值(Web 浏览器中的window
,Node.js 中的global
, ETC。)。因此,事实上,您每次调用函数时都会设置该函数的
this
。PS 调用
steve.apply(null, [])
相当于调用steve()
。You cannot, because of the way
this
works in JavaScript. Read on:Going by the "Entering An Execution Context" section of the ECMAScript spec: when you call a function, the value of
this
is determined by what's to it's left (called the activation object) . Let's create a function called steve, and put him in an object:…when we call steve as
obj.method()
, histhis
isobj
, becauseobj
was the activation object.The tricky case is when nothing is to the left of a function call:
There's nothing to the left of the function call — effectively, it's
null
— so the value ofthis
is set to a default value of the global object (window
in web browsers,global
in Node.js, etc.).So you are, in fact, setting a function's
this
every time you call it.P.S. calling
steve.apply(null, [])
is equivalent to callingsteve()
.注意,在 ES6 中你还可以这样写:
这里
this
变成func
内的Window
,这是没有用的。但是如果你在
func
里面写:this
将会是obj
。这可能是我 5 年前一直在寻找的功能,但那是 5 年前的事了,所以谁还记得:)
Note that in ES6 you can also write:
Here
this
becomesWindow
insidefunc
, which is of no use. But if you write:this
in insidefunc
will beobj
.This may be the feature i was looking for 5 years ago, but that was 5 years ago, so who remembers :)
调用函数时,
this
的值并不神秘,具体取决于它如何“正常”调用(没有call
或apply
):所以为了避免“更改”
this
的值,只需将正确的值传递给apply
,具体取决于您如何“正常”调用它:The value of
this
is no mystery when calling a function, depending on how it is called "normally" (withoutcall
orapply
):So to avoid "changing" the value of
this
, just pass in the correct value toapply
, depending on how you would call it "normally":如果您单独调用函数,请传递
null
。如果您调用的函数是对象的方法,请传递该对象。
为了匹配 this 的工作方式。
If you are calling a function alone, pass
null
.If you are calling a function that is a method of an object, pass that object.
In order to match a how this works.
你想要的没有多大意义。
在 ecmascript 标准中
this
是这样定义的:函数执行上下文的 thisBinding 解析是这样指定的:
换句话说:要么您指定它,要么它自动设置为全局对象。它永远不会从其父作用域继承 this 绑定,因此将父作用域的 thisBindiong 赋予其子作用域的唯一方法是将
self = this
What you want doesn't make much sense.
In the ecmascript standard
this
is defined this way:The thisBinding resolution for a functions execution context is specified this way:
In other words: either you specify it or it is automatically set to the global object. It will never inherit the this binding from it's parent scope, so the only way to give the thisBindiong of the parent scope to it's children is either by
self = this