我正在尝试在原型中创建一个属性,以便它在所有实例中都可用(不能将它放在构造函数中,并且宁愿不通过 instance.propertyname = *; 创建它,如果可能的话。举个例子下面的代码:
function A() {
}
A.prototype.constructor = A;
Object.defineProperty(A.prototype, 'B', {
writable: true,
enumerable: true,
value: null
});
var a = new A();
a.B = 'test';
var b = new A();
var c = new A();
b.B = '2';
console.log(a,b,c);
产生输出:
A {B: "test", B: null} A {B: "2", B: null} A {B: null}
我如何让它改为产生:
A {B: "test"} A {B: "2"} A {B: null}