3

このコードの最大呼び出しスタック サイズでエラーが発生します。

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);

エラー

RangeError: Maximum call stack size exceeded
4

1 に答える 1

4

これsetは、割り当てが発生するたびに関数が実行されるためです。再帰コードを定義しています。それ以外の別のプロパティを定義すると、authenticationそのエラーは発生しません。

Object.defineProperty(ValueObject.prototype, "authentication", {
    get : function () {
        return this._authentication;
    },
    set : function (val) {
        this._authentication = val;
    }
});
于 2016-07-07T22:29:50.550 に答える