0

I would like to be able to make a dynamic objet in nodejs, which when a poperty is accessed, a function is called with the property name. As an example is far more explicit than a text:

// what I am seeking
obj.get = function(name) {
   return "Hello, "+ name
}
console.log(obj.foo); // Excpected : Hello, foo
console.log(obj.bar); // Excpected : Hello, bar

EDIT:

I know there is Proxy, but they don't exist in node.js. How to do that in nodejs?

Vinz243
  • 9,654
  • 10
  • 42
  • 86
  • Without the ES6 proxy feature, there's really no good way to do this. The language simply doesn't have support for such a thing. *edit* you can always invoke Node with `--harmony_proxies true` ... – Pointy May 28 '14 at 18:12
  • @Pointy maybe, but please reopen since it is no more a duplicate. – Vinz243 May 28 '14 at 18:13
  • It is a duplicate - whether it's in the browser or Node it's the same issue. Check out the `--harmony_proxies` V8 option for Node. – Pointy May 28 '14 at 18:14
  • OK, thanks but it isn't the same duplicate then : https://stackoverflow.com/questions/10665892/enable-harmony-proxies-in-nodejs – Vinz243 May 28 '14 at 18:16
  • Ha I was just about to link that question here :) I'll add a comment to the other one - I think it's a good question with well written answers so it works as a focal point for this topic. – Pointy May 28 '14 at 18:19

0 Answers0