var person = {
sayName() { console.log(this.name);
},
get firstName() { return "Nicholas";
}
};
person.sayName.name // "sayName"person.firstName.name // "get firstName"上面的代碼是阮神的ES6一書中,但是我的運(yùn)行結(jié)果person.firstName.name為undefined,因為person.firstName只是一個字符串而不是函數(shù)。附上鏈接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('數(shù)值太小了!'); } 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));
ES6函數(shù)的name屬性
有只小跳蛙
2018-10-19 15:16:17