将方法operator()分配给对象
我在现实世界的项目中遇到这个问题。
我至少需要像史蒂夫所说的那样“将方法operator()分配给一个对象”。 重新分配不是一个选项,因为该对象太复杂并且不是在 Javascript 中创建的。
我有一个对象 x (但不是函数)。 我想以这种方式调用一个方法,
x()
我尝试添加一个 apply
和一个 call
方法,但它不起作用。 Javascript 似乎总是记住“不是一个函数”(这是我总是得到的错误)。 有没有办法“破解”Spidermonkey 引擎来实现这一目标?
I'm having this problem in a real-world Project.
How to convert an "object" into a function in JavaScript?
I need at least as Steve said "to assign a method operator() to an object".
Re-assign is not an option, because the object is too complex and it's not created within Javascript.
I have an object x (but not a function).
And I want to call a method this way
x()
I tried to add an apply
and a call
method but it didn't work. It seems that Javascript remember always that "is not a function" (that's the error I obtain always).
Is there a way to "hack" the Spidermonkey engine to achieve this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,正如人们在该问题中告诉您的那样,您不能追溯从不是函数的东西中创建函数。如果可以(在 JavaScript 中),我会非常谨慎地尝试使用“...不是在 JavaScript 中创建的”对象。
真正的最终目标是什么?因为我怀疑有更好的方法可以到达那里。
Well, as people told you in that question, you can't retroactively make a function out of something that isn't a function. And if you could (in JavaScript), I'd be very leery of trying it with an object that is "...not created within JavaScript."
What is the real, end goal? Because I suspect there's a better way to get there.