Accessors Back

  • Accessors functions for properties are not required.

1. Use getVal() and setVal(val)

/**
 * bad
 */
dragon.age();
dragon.age(25);

/**
 * good
 */
dragon.getAge();
dragon.setAge(25);

2. Use isVal() or hasVal()

  • To use isVal() or hasVal() when the property is a boolean.
/**
 * bad
 */
if (!dragon.age()) {
}

/**
 * good
 */
if (!dragon.hasAge()) {
}

3. Use get() and set()

  • It's ok to create get() and set(), but be consistent.
class Aleen {
    constructor(options = {}) {
        const lightsaber = options.lightsaber || 'blue';
        this.set('lightsaber', lightsaber);
    }

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

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

results matching ""

    No results matching ""