1
只在Chrome中進行測試,我的應用程序不需要在任何其他瀏覽器中工作。爲什麼Object.defineProperty定義的屬性的行爲在使用之前被訪問時會發生變化?
例如,在下面的代碼(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
這到底是怎麼回事?
順便提一下,您錯誤地輸入了'A.prototype.constrcutor'。我不知道你爲什麼要改變它 - 你需要使用該屬性跨瀏覽器? – 2013-02-22 14:03:07
謝謝,我糾正了這個問題,儘管如此。 – CoryG 2013-02-22 14:04:52