Long story short: I'm in a situation where I'd like a PHP-style getter, but in JavaScript.
My JavaScript is running in Firefox only, so Mozilla specific JS is OK by me.
The only way I can find to make a JS getter requires specifying its name, but I'd like to define a getter for all possible names. I'm not sure if this is possible, but I'd very much like to know.
If you really need an implementation that works, you could "cheat" your way arround by testing the second parameter against
undefined
, this also means you could use get to actually set parameter.I ended up using a nickfs' answer to construct my own solution. My solution will automatically create get_{propname} and set_{propname} functions for all properties. It does check if the function already exists before adding them. This allows you to override the default get or set method with our own implementation without the risk of it getting overwritten.
If you are coding in ES6 you can combine proxy and class to have a nice looking code like php:
this binds to the handler, so you can use this instead of target.
Note: unlike PHP, proxy handles all the property request.
You can check which browsers support proxy http://caniuse.com/#feat=proxy and class http://caniuse.com/#feat=es6-class. Node 8 support both.
If you're looking for something like PHP's
__get()
function, I don't think Javascript provides any such construct.The best I can think of doing is looping through the object's non-function members and then creating a corresponding "getXYZ()" function for each.
In dodgy pseudo-ish code:
Javascript 1.5 does have getter/setter syntactic sugar. It's explained very well by John Resig here
It's not generic enough for web use, but certainly Firefox has them (also Rhino, if you ever want to use it on the server side).
The closest you can find is __noSuchMethod__, which is JavaScript's equivalent of PHP's __call().
Unfortunately, there's no equivalent of __get/__set, which is a shame, because with them we could have implemented __noSuchMethod__, but I don't yet see a way to implement properties (as in C#) using __noSuchMethod__.