使用 Function.prototype.apply 设置 javascript 回调范围
每次在 JavaScript 中声明回调时都必须手动设置对象的范围,这很令人沮丧,但这是现实。我想知道是否可以通过传递 [mycallback].apply 作为回调,并将作用域对象作为参数来实现,如下所示:
var f = function() { console.log(this.x); };
var o = {x: 3};
setTimeout(f.apply, 1000, o);
据我所知,这应该以 o 作为作用域来调用 f,但 Chrome 给出了我“未捕获的类型错误:在 [object DOMWindow] 上调用了 Function.prototype.apply,它是一个对象而不是函数”。为什么这不起作用?
It's frustrating to have to manually set the scope of an object every time I declare a callback in JavaScript, but it's a fact of life. I wondered if I could do it by passing [mycallback].apply as the callback, and the scope object as an argument, like so:
var f = function() { console.log(this.x); };
var o = {x: 3};
setTimeout(f.apply, 1000, o);
As far as I can tell, this should call f with o as the scope, but instead Chrome gives me "Uncaught TypeError: Function.prototype.apply was called on [object DOMWindow], which is a object and not a function". Why doesn't this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
出于同样的原因,您首先需要“设置范围”。只有
apply
函数被发送到setTimeout
,它与函数f
的关联丢失了。因此,Javascript 将全局对象window
分配给this
,就像在任何其他情况下一样。有趣的是,
apply
虽然是一个本机函数,但在某些方面并不特殊或神奇,并且其行为方式与this< 设置中用户定义的函数一致。 /代码> 变量。
For the same reason you need to "set the scope" in the first place. Only the
apply
function is sent tosetTimeout
, its association with functionf
is lost. Thus Javascript assigns the global object,window
, tothis
, as it would in any other case.It is interesting to note that
apply
, while being a native function, is not special or magical in some way, and behaves in a way consistent to user defined functions in the setting of thethis
variable.@MooGoo 的答案是正确的,但也许需要更多解释。
当您像这样在
f
上调用函数apply
时:...那么您就在
f< 的上下文中执行
apply
/代码>。但是,当您将对
apply
的引用传递给函数时,如下所示:...这就是您所做的一切:传递对函数
f.apply
的引用。这相当于传递Function.prototype.apply
,因为:与
f
的任何连接都会在window.setTimeout
中丢失。它接收对Function.prototype
的通用apply
函数的引用。而已。没有上下文。因此,与未设置显式上下文的任何其他情况一样,将使用
window
作为其上下文对象来调用apply
函数。@MooGoo's answer is correct, but perhaps more explanation is needed.
When you call the function
apply
onf
like this:...then you're executing
apply
in the context off
.But when you pass a reference to
apply
to a function, like this:...that's all you're doing: passing a reference to the function
f.apply
. This is equivalent to passingFunction.prototype.apply
because:Any connection to
f
is lost inwindow.setTimeout
. It receives a reference to the genericapply
function ofFunction.prototype
. Nothing more. No context.Therefore, as in any other case where an explicit context is not set, the
apply
function is called withwindow
as its context object.试试这个:
对我有用。
设置超时需要一个函数。但由于 apply 是一个内置函数,并且您看不到它背后的本机代码,因此它可能无法充当“函数”对象。
Try this:
Works for me.
Set timeout expects a function. But since apply is a built in function and you can not see the native code behind it, it may not act as a "function" object.