简体   繁体   English

在Node.js中模仿PHP的__get(),__ set()和__call()魔术方法

[英]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. 我想知道是否有办法在Node中模仿PHP的魔术方法__get()和__set()。 From this question: JavaScript getter for all properties I know you can do it in Rhino, but Node's built on V8. 从这个问题: 所有属性的JavaScript getter我知道你可以在Rhino中做到,但Node是在V8上构建的。 Does V8 have some way of doing this? V8有办法做到这一点吗?

I believe you're out of luck, at least as of March 2010 . 我相信你运气不好,至少从2010年3月开始 At least you have __defineGetter__ and __defineSetter__ , though I realize that's not the same thing. 至少你有__defineGetter____defineSetter__ ,虽然我意识到这不是一回事。 In general I think using __noSuchMethod__ / __get / method_missing is not good since it makes the code harder to read. 一般来说,我认为使用__noSuchMethod__ / __get / method_missing并不好,因为它使代码更难阅读。 Consider trying to get by without it and see if it makes your code clearer. 考虑试图在没有它的情况下过去,看看它是否能让你的代码更清晰。

No answer? 没有答案? Check out nowjs. 看看nowjs。 They seem to have found a way to make __get() and __set() work in JavaScript. 他们似乎已找到一种方法使__get()和__set()在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) 编辑:检查监视所有JavaScript对象属性(魔术getter和setter)

看看我的下面的答案 ,解释如何使用ES6代理解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM