Object.defineProperty Error

807 views Asked by At

I have to find an equivalent code to :

Object.defineProperty(object, sProperty, vValue); 

because i'm working with an old js interpreter which doesn't support ECMA Javascript5, so i'm trying to switch to this code :

if(vValue.get)
  object["get "+ sProperty] = vValue.get;
 if(vValue.set)
  object["set " + sProperty] = vValue.set;

but it doesn't work. PS : my vValue is : vValue

1

There are 1 answers

2
AudioBubble On BEST ANSWER

Assuming vValue is of the form value: x, then

function myDefineProperty(object, sProperty, vValue) {
  object[sProperty] = vValue.value;
}

If vValue is a property descriptor including get and set, then you could experiment with using __defineGetter__ and __defineSetter__ if they are available in your environment:

function myDefineProperty(object, sProperty, vValue) {
  object.__defineGetter__(sProperty, vValue.get);          
  object.__defineSetter__(sProperty, vValue.set);
}

For a more full-featured implementation, see the polyfill at https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties#Polyfill.