これを 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
ここで何が起こっているのですか?