【发布时间】:2017-03-05 20:03:25
【问题描述】:
我尝试通过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?
【问题讨论】:
标签: javascript