I was wondering if there is a way to imitate PHP's magic methods __get() and __set() in Node. From this question: JavaScript getter 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 was wondering if there is a way to imitate PHP's magic methods __get() and __set() in Node. From this question: JavaScript getter 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?
Share Improve this question edited May 23, 2017 at 12:11 CommunityBot 11 silver badge asked Dec 31, 2010 at 19:54 MattMatt 22.9k25 gold badges85 silver badges118 bronze badges3 Answers
Reset to default 2No 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)
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.
Have a look at my following answer that explains how one might tackle this with the use of ES6 proxies.