The JavaScript code adheres to the style guide found here, but with some exceptions:
V8 support for native ES6 modules is under development, so for now use require
like you would in Node.
All other ES6 features (classes, destructuring, default parameters, etc.) can be used freely.
You're free to use for-of
and for-in
as necessary, however it's better to use for (let value of object)
for values and for (let key of Object.keys(object))
for keys.
You can still use for-in
, just be sure you know the catch when using it.
You can use 'dangling' underscores to denote a private member on an object or class.
Some APIs were written before this style was adopted which use underscores for private members and probably won't be changed for compatibility.
However, for any new APIs, it'd be preferred to use a Symbol
instead, like:
const somePrivateVarName = Symbol('somePrivateVarName');
class Demo {
constructor() {
this[somePrivateVarName] = 'my value';
}
getMyPrivateVar() {
return this[somePrivateVarName];
}
}
You can (and should) use ES6 get
and set
in new APIs, it's already used in various runtime.js APIs.
You cannot reassign functions parameters, but you can assign to properties of function parameters.
// yes
const demo = (myObject) => {
myObject.someProperty = 'my value';
// ...
}
// no
const demo = (myObject) => {
myObject = {
someProperty: 'my value'
};
// ...
}
The invocation should be outside the parentheses, not inside.
// yes
(function() {
// ...
})();
// no
(function() {
}());