Naming Conventions
Single letter names
- Avoid single letter names. Be descriptive with your naming.
function q() {
}
function query() {
}
Name casing
const OBJEcttsssss = {};
const this_is_my_object = {};
function c() {}
const thisIsMyObject = {};
function thisIsMyFunction() {}
function user(options) {
this.name = options.name;
}
const bad = new user({
name: 'nope',
});
class User {
constructor(options) {
this.name = options.name;
}
}
const good = new User({
name: 'yup',
});
- Use camelCase when you export-default a function. Your filename should be identical to your function's name.
function makeStyleGuide() {
}
export default makeStyleGuide;
- Use PascalCase when you export a singleton / function library / bare object.
const AirbnbStyleGuide = {
es6: {
}
};
export default AirbnbStyleGuide;
Name prefixes
this.__firstName__ = 'Panda';
this.firstName_ = 'Panda';
this._firstName = 'Panda';
this
reference
- Don't save references to
this
. Use arrow functions or Function#bind. jscs: disallowNodeTypes
function foo() {
const self = this;
return function () {
console.log(self);
};
}
function foo() {
const that = this;
return function () {
console.log(that);
};
}
function foo() {
return () => {
console.log(this);
};
}
File naming
- If your file exports a single class, your filename should be exactly the name of the class.
class CheckBox {
}
export default CheckBox;
import CheckBox from './checkBox';
import CheckBox from './check_box';
import CheckBox from './CheckBox';