仅在 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
这里发生了什么?