1 回答
TA贡献1898条经验 获得超8个赞
使用Object.defineProperties(一次定义多个属性)和this. 该writable属性定义目标对象中定义的属性是否仅可读,默认为false
另请参阅MDN 上的文档:
writable
true 当且仅当与属性关联的值可以用赋值运算符更改时。
默认为false.
class Komplex {
constructor(real, imag) {
if (real === undefined && imag === undefined) {
real = 0
imag = 0
} else if (imag === undefined) {
imag = 0
} else if (typeof real !== "number" || typeof imag !== "number") {
real = 0
imag = 0
}
Object.defineProperties(this, {
real: {
value: real
},
imag: {
value: imag
}
});
}
}
(function() {
'use strict';
let val = new Komplex(1, 2);
val.real = 5;
})();
添加回答
举报
