Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Parameters for Object.defineProperty Setter Function?

Is it possible to have multiple parameters for Object.defineProperty setter function?

E.G.

var Obj = function() {
  var obj = {};
  var _joe = 17;

  Object.defineProperty(obj, "joe", {
    get: function() { return _joe; },
    set: function(newJoe, y) {
      if (y) _joe = newJoe;
    }
  });

  return obj;
}

I don't get any errors from the syntax, but I can't figure out how to call the setter function and pass it two arguments.

like image 513
rob-gordon Avatar asked Sep 10 '13 19:09

rob-gordon


1 Answers

Is it possible to have multiple parameters for Object.defineProperty setter function?

Yes, but it's not possible to invoke them (other than Object.getOwnPropertyDescriptor(obj, "joe").set(null, false)). A setter is invoked with the one value that is assigned to the property (obj.joe = "doe";) - you cannot assign multiple values at once.

If you really need them (for whatever reasons), better use a basic setter method (obj.setJoe(null, false)).

like image 103
Bergi Avatar answered Sep 28 '22 08:09

Bergi