1

これを Chrome でのみテストしました。私のアプリケーションは他のブラウザで動作する必要はありません。

たとえば、次のコード ( JSFiddle ) では:

function A(a) {
    document.write(this.B);
    this.A = a;
    this.B = a;
}
function A_C(a) {
    this.A = a;
    this.B = a;
}
A.prototype.constructor = A;
A.prototype.C = A_C;
Object.defineProperty(A.prototype, 'B', {
    writeable: true,
    enumerable: true,
    value: '0'
});

var B = A;

var C = new A('A');
var D = new B('B');

document.write(C.A);
document.write(D.A);
document.write(C.B);
document.write(D.B);

C.C('C');
D.C('D');

document.write(C.A);
document.write(D.A);
document.write(C.B);
document.write(D.B);

出力は次のとおりです。

00AB00CD00

それ以外の:

00ABABCDCD

一方、次のコード ( JSFiddle ) では:

function A(a) {
    this.A = a;
    this.B = a;
}
function A_C(a) {
    this.A = a;
    this.B = a;
}
A.prototype.constructor = A;
A.prototype.C = A_C;
Object.defineProperty(A.prototype, 'B', {
    writeable: true,
    enumerable: true,
    value: '0'
});

var B = A;

var C = new A('A');
var D = new B('B');

document.write(C.A);
document.write(D.A);
document.write(C.B);
document.write(D.B);

C.C('C');
D.C('D');

document.write(C.A);
document.write(D.A);
document.write(C.B);
document.write(D.B);

出力は次のとおりです。

ABABCDCD

ここで何が起こっているのですか?

4

1 に答える 1

1

入力ミスwritable:

writable: true

期待どおりに動作します。フィドル

writablefalseデフォルトであるため、名前を間違って入力してもfalse.


書き込み不可のプロパティを設定し、プロトタイプをオーバーライド/シャドウする方法は意味がありません。Chrome 実装のバグのようです。このバグのある動作は、Firefox では再現できません。

于 2013-02-22T14:11:44.293 に答える