Per the
ES6 style guide, I'd like to propose some ECMAScript 2015 features which we already use in Chromium code. If anyone has concerns about a particular feature, we can spin it off into its own thread -- I'm just dumping them all in one because I'd rather not spam the list with N threads unless we need to discuss something in detail.
These features are all ones we've used before (and removed to adhere to the new ES6 style guide) or are even now using in Chromium.
Rationale: More convenient than for (var i = 0; i < arr.length; i++) {. Safer than for...in for things like Arrays.
Example usage:
var fields = ['foo', 'bar', 'baz', 'bat'];
// Traditional:
for (var i = 0; i < fields.length; i++) {
var field = fields[i];
if (this.settings[field] != this.oldSettings[field])
this.sendFieldUpdate(field, this.settings[field])
}
// With for..of:
for (var field of fields) {
if (this.settings[field] != this.oldSettings[field])
this.sendFieldUpdate(field, this.settings[field])
}
Overview: Copies enumerable own properties from source object(s) to a target object.
Rationale: Useful for creating shallow copies of objects, especially in Polymer where setting an object has different effects from setting a value of an object.
Example usage:
// Copies values from |prefs| to populate the dialog. Cancelling the
// dialog will not update the original prefs object.
FooDialog.prototype.applyPrefs = function(prefs) {
var newPrefs = Object.assign({}, prefs);
this.prefs = newPrefs;
};
The other Object static methods are not quite as useful right now, but we may propose Object.is() later.
Overview: Creates a new Array instance from an array-like or iterable object.
Rationale: Useful for using array methods on things that look like Arrays but aren't.
Example usage:
Array.from(document.querySelectorAll('div')).map(function(div) {
div.style.backgroundColor = getRandomColor();
});
We can separately propose the Array.prototype methods that are already used in Chrome but restricted by the style guide.
Overview: Returns true only if a value is of Number type and is NaN.
Rationale: Checking for NaN is hard or unsafe otherwise.
NaN compares unequal to itself:
NaN == NaN // false
NaN === NaN // false
The global isNaN() function implicitly converts its parameter to a Number:
isNaN(Number.NaN); // true
isNaN(undefined); // true
isNaN(null); // false
isNaN(''); // false
Whereas the ES6 Number.isNaN() function is more robust:
Number.isNaN(Number.NaN); // true
Number.isNaN(undefined); // false
Number.isNaN(null); // false
Number.isNaN(''); // false