开发者

Imitate PHP's __get(), __set() & __call() magic methods in Node.js

I was wondering if there is a way to imitate PHP's magic methods __get() and __set() in Node. From this question: JavaScript get开发者_JAVA百科ter for all properties I know you can do it in Rhino, but Node's built on V8. Does V8 have some way of doing this?


I believe you're out of luck, at least as of March 2010. At least you have __defineGetter__ and __defineSetter__, though I realize that's not the same thing. In general I think using __noSuchMethod__/__get/method_missing is not good since it makes the code harder to read. Consider trying to get by without it and see if it makes your code clearer.


No answer? Check out nowjs. They seem to have found a way to make __get() and __set() work in JavaScript. I can't figure out how they do it from the source code. :(

EDIT: Check out Monitor All JavaScript Object Properties (magic getters and setters)


Have a look at my following answer that explains how one might tackle this with the use of ES6 proxies.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜