var person = {
sayName() { console.log(this.name);
},
get firstName() { return "Nicholas";
}
};
person.sayName.name // "sayName"person.firstName.name // "get firstName"上面的代码是阮神的ES6一书中,但是我的运行结果person.firstName.name为undefined,因为person.firstName只是一个字符串而不是函数。附上链接http://es6.ruanyifeng.com/#do...还有,我按照ES6这种和ES5的get,set写出来的对象也有差异: var cart = { _wheels: 4, get wheels () { return this._wheels; }, set wheels (value) { if (value < this._wheels) { throw new Error('数值太小了!'); } this._wheels = value; } } var book={ _year:4 } Object.defineProperty(book,'year',{ get:function(){ return this._year+1 }, set:function(m){ _year = m*2; } }) console.log(cart); console.log(Object.keys(cart)); console.log(Object.keys(book));
添加回答
举报
0/150
提交
取消