23.2?如果你需要存取函數(shù)時(shí)使用?getVal()
?和?setVal('hello')
。
// bad
dragon.age();
// good
dragon.getAge();
// bad
dragon.age(25);
// good
dragon.setAge(25);
23.3?如果屬性是布爾值,使用?isVal()
?或?hasVal()
。
// bad
if (!dragon.age()) {
return false;
}
// good
if (!dragon.hasAge()) {
return false;
}
23.4?創(chuàng)建?get()
?和?set()
?函數(shù)是可以的,但要保持一致。
class Jedi {
constructor(options = {}) {
const lightsaber = options.lightsaber || 'blue';
this.set('lightsaber', lightsaber);
}
set(key, val) {
this[key] = val;
}
get(key) {
return this[key];
}
}
更多建議: