TreeviewCopyright © aleen42 all right reserved, powered by aleen42
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()
orhasVal()
when the property is aboolean
.
/**
* bad
*/
if (!dragon.age()) {
}
/**
* good
*/
if (!dragon.hasAge()) {
}
3. Use get()
and set()
- It's ok to create
get()
andset()
, 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];
}
}