在 Ruby 中,您可以捕获对缺失方法的调用并动态定义它。
我想在 JavaScript 中实现的目标是拥有一个没有方法的对象。我希望将缺少的方法转换为对emit()的调用:
app.isReady() -> app.emit("isReady")
soldier.kills() -> soldier.emit("kills")
我认为最好捕获缺少的方法错误并运行emit(methodName),而不是在运行时定义所有方法(来自固定列表)。这样,如果一个对象有数百或数千个事件,我们就不会产生性能开销。
最好的方法是什么?
更新:这是一个 API 设计,所以我宁愿不参与:
try {
app.isReady()
} catch(e) {
...
}
我想知道如何在幕后完成此操作,以便用户可以像往常一样使用方法。
In Ruby, you can capture a call to a method which is missing and define it on the fly.
What I wanna accomplish in JavaScript is to have an object with no methods. I want a missing method to be translated into a call to emit():
app.isReady() -> app.emit("isReady")
soldier.kills() -> soldier.emit("kills")
I think it's better to capture the missing method error and run emit(methodName) rather than defining all methods (from a fixed list) at runtime. That way we don't have performance overhead if there are hundreds or thousands of events for an object.
What is the best way to do this?
UPDATE: This is an API design so I rather stay out of:
try {
app.isReady()
} catch(e) {
...
}
I want to know how I can accomplish this behind the scenes so the users can use methods as usual.
发布评论
评论(4)
我认为认为向对象添加方法的性能开销小于将方法调用转换为发出调用的性能开销是一个巨大的误解。
但是,您无法在 ES5 中实现此功能,
但是可以使用 Harmony 代理 来实现此功能。
我建议查看模拟
__noSuchMethod__
。我相信 ES6 代理是实验性的,可以在 V8 中打开,因此您现在可以将它们与 Node.js 一起使用。
I think it's a massive misconception to think the performance overhead of adding methods to an object is smaller then the performance overhead of converting method invocations into emit calls.
However you cannot implement this feature in ES5
One could however implement this using Harmony proxies.
I recommend looking at simulating
__noSuchMethod__
.I believe ES6 proxies are experimental and can be turned on in V8 so you could use them with node.js today.
在此阶段不可能始终如一地做到这一点,除非您可以保证您的应用程序只能在 Mozilla 上运行,在这种情况下 noSuchMethod 就是您所追求的。
据我所知,其他浏览器还没有实现这一点。
It's not possible to do that consistently at this stage, unless you can guarantee your app will only run on Mozilla, in which case noSuchMethod is what you're after.
As far as I know, none of the other browsers implement this yet.
通过以下过程在函数指针上使用 RegExp
test
:function
检查类型(后备例如:
参考资料
其他语言中是否有与 Ruby 的 method_missing 等效的方法?
JSPerf:方法丢失
处理不同语言中对缺失方法的调用
jQuery 是一个 Monad
Smalltalk 中的消息转发
对齐 Smalltalk 和 JavaScript 类的性能
Perl 可以成为 Smalltalk 吗?
学习 Smalltalk 如何让您成为更好的开发人员
Smalltalk-80 中的反射设施
12.1。 QMetaObject - 元对象模式
对象编程和面向对象编程
关于 JavaScript 你应该了解的事情
响应未知方法调用
使用 OnMissingMethod() 创建线程安全组件
关于 ECMAScript Reflection API 的设计 (pdf)
不理解
Use a RegExp
test
on the function pointer by using the following process:function
For example:
References
Are there equivalents to Ruby's method_missing in other languages?
JSPerf: Method Missing
Handling a call to a missing method in different languages
jQuery is a Monad
Message forwarding in Smalltalk
Performance from Aligning Smalltalk and JavaScript Classes
Can Perl be Smalltalk?
How learning Smalltalk can make you a better developer
Reflective Facilities in Smalltalk-80
12.1. QMetaObject - The Meta Object Pattern
Object Programming and Object-Oriented Programming
Things you should know about JavaScript
Respond to an Unknown Method Call
Creating Thread-Safe Components With OnMissingMethod()
On the design of the ECMAScript Reflection API (pdf)
Does Not Understand
我创建了一个 Node.js 包来处理您的情况。它称为 auto-object。
一目了然:
此外,这也适用于课堂。
I've create a Node.js package to deal with your situation. It's called auto-object.
Here's a glance:
What's more, this works with class too.