2
我试着通过Object.assign
来定义构造函数getter和setter:Object.assign getter和setter方法在构造
function Class() {
Object.assign(this, {
get prop() { console.log('call get') },
set prop(v) { console.log('call set') },
});
}
var c = new Class(); // (1) => 'call get'
console.log(c.prop); // (2) => undefined
c.prop = 'change';
console.log(c.prop); // (3) => 'change'
问题:
(1)为什么调用getter时?
(2)为什么不调用getter?
(3)为什么setter被忽略?