3

I'm getting an error on Maximum call stack size for this code.

function ValueObject() {
}

ValueObject.prototype.authentication;

Object.defineProperty(ValueObject.prototype, "authentication", {
    get : function () {
        return this.authentication;
    },
    set : function (val) {
        this.authentication = val;
    }
});

var vo = new ValueObject({last: "ac"});
vo.authentication = {a: "b"};
console.log(vo);

Error

RangeError: Maximum call stack size exceeded
user2727195
  • 7,122
  • 17
  • 70
  • 118
  • That's because the `set` function is executed each time the assignment happens. There is no point in using `defineProperty` in your code. What you are defining is the pre-defined behavior. – Ram Jul 07 '16 at 22:22

1 Answers1

4

That's because the set function is executed each time the assignment happens. You are defining a recursive code. If you define a different property other than authentication then you don't get that error.

Object.defineProperty(ValueObject.prototype, "authentication", {
    get : function () {
        return this._authentication;
    },
    set : function (val) {
        this._authentication = val;
    }
});
Ram
  • 143,282
  • 16
  • 168
  • 197
  • is worth to mention that the exception `RangeError: Maximum call stack size exceeded` also happens on get operation – Victor Dec 13 '19 at 15:52