본문 바로가기

개발/Javascript

억세서(Accessors)

억세서(Accessors)

  • Accessor functions for properties are not required.

  • 프로퍼티를 위한 억세서 (Accessor) 함수는 필수는 아닙니다.

  •  If you do make accessor functions use getVal() and setVal('hello').

  • 억세서 함수가 필요한 경우, getVal() 이나 setVal('hello') 로 해주십시오.

    // bad
    dragon.age();
    
    // good
    dragon.getAge();
    
    // bad
    dragon.age(25);
    
    // good
    dragon.setAge(25);
  • If the property is a boolean, use isVal() or hasVal().

  • 프로퍼티가 boolean 인 경우, isVal() 이나 hasVal() 로 해주십시오.

    // bad
    if (!dragon.age()) {
      return false;
    }
    
    // good
    if (!dragon.hasAge()) {
      return false;
    }
  • It's okay to create get() and set() functions, but be consistent.

  • 일관된 경우, 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];
      }
    }


'개발 > Javascript' 카테고리의 다른 글

V8 자바 스크립트 엔진  (0) 2018.03.11
이벤트(Events)  (0) 2018.03.11
명명규칙(Naming Conventions)  (0) 2018.03.11
형변환과 강제(Type Casting & Coercion)  (0) 2018.03.11
세미콜론(Semicolons)  (0) 2018.03.11