Skip to content

Latest commit

 

History

History
50 lines (38 loc) · 712 Bytes

File metadata and controls

50 lines (38 loc) · 712 Bytes

属性访问和修改

  • 属性读取函数和设置函数使用getVal()setVal('hello')
// bad
dragon.age();

// good
dragon.getAge();

// bad
dragon.age(25);

// good
dragon.setAge(25);
  • 如果属性是布尔值,使用 isVal()hasVal()
// bad
if (!dragon.age()) {
  return false;
}

// good
if (!dragon.hasAge()) {
  return false;
}
  • 创建get()set()函数是可以的,但要保持一致。
class Jedi {
  constructor(options = {}) {
    const lightsaber = options.lightsaber || 'blue';
    this.set('lightsaber', lightsaber);
  }

  set(key, val) {
    this[key] = val;
  }

  get(key) {
    return this[key];
  }
}