ES6 in Chromium proposals: for...of, Object.assign, Array.from, Number.isNaN

93 views
Skip to first unread message

Michael Giuffrida

unread,
Jul 21, 2017, 4:08:31 PM7/21/17
to Chromium-dev
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.



Overview: Iterates over iteratable collections like Array and NodeList.

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])
}

Supported in iOS9 (for object types supported in iOS9).




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.



Feature: Array.from()

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();
});

Supported in iOS 9 (for object types supported in iOS9).

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

Mike Frysinger

unread,
Jul 21, 2017, 4:19:57 PM7/21/17
to Michael Giuffrida, Chromium-dev
odd ... why isn't this doc linked/part of the existing style guides ?  it's under docs/ instead of styleguide/, and the JS styleguide makes no mention of this anywhere that i can find.

back to your requests, all of those features sound good to me.  they aren't language based (like async/await), so if really need be (for something like iOS), we can easily polyfill.  i use some of those already :).
-mike

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/a0305b6a-9727-4d34-96b8-773e144e3df7%40chromium.org.

Dan Beam

unread,
Jul 21, 2017, 4:57:32 PM7/21/17
to vap...@chromium.org, Michael Giuffrida, Chromium-dev, Demetrios Papadopoulos
On Fri, Jul 21, 2017 at 1:18 PM, Mike Frysinger <vap...@chromium.org> wrote:
odd ... why isn't this doc linked/part of the existing style guides ?  it's under docs/ instead of styleguide/, and the JS styleguide makes no mention of this anywhere that i can find.

src/docs/es6_chromium.md was written before src/styleguide/web/.  When/how we'd use ES6 features in Chromium wasn't really clear at the time, but has recently been prioritized and tooling has gotten way better.

It would make sense (IMO) to move/integrate the ES6 stuff into styleguide/ now that there's more dedication to ES6 in Chromium (mainly from +dpapad@ and team).

-- Dan
 

dpapad

unread,
Jul 21, 2017, 5:38:09 PM7/21/17
to Chromium-dev, vap...@chromium.org, mich...@chromium.org, dpa...@chromium.org
All proposed features from @michaelpg sound reasonable to me.

About for..of: since it introduces a new syntax, it would require plugging it in in a WebUI page (preferable one using Polymer), to ensure that all tools can process it correctly, similar to the approach I've followed with const/let and MD Downloads here.
For the remaining features, we just have to ensure that Closure compiler understands them, and update the externs definitions if not.


On Friday, July 21, 2017 at 1:57:32 PM UTC-7, Dan Beam wrote:
On Fri, Jul 21, 2017 at 1:18 PM, Mike Frysinger <vap...@chromium.org> wrote:
odd ... why isn't this doc linked/part of the existing style guides ?  it's under docs/ instead of styleguide/, and the JS styleguide makes no mention of this anywhere that i can find.

src/docs/es6_chromium.md was written before src/styleguide/web/.  When/how we'd use ES6 features in Chromium wasn't really clear at the time, but has recently been prioritized and tooling has gotten way better.

It would make sense (IMO) to move/integrate the ES6 stuff into styleguide/ now that there's more dedication to ES6 in Chromium (mainly from +dpapad@ and team).

I'll look into either linking from src/styleguide/ to src/docs/es6_chromium.md, or potentially moving it to src/styleguide.

dpapad

unread,
Aug 3, 2017, 4:59:43 PM8/3/17
to Chromium-dev, vap...@chromium.org, mich...@chromium.org, dpa...@chromium.org
There have been no objections for these proposals, so I prepared a CL to move Array, Object, Number prototype/static methods to the "Allowed features". "for..of" needs additional testing, so I'll leave that for a separate CL.
Reply all
Reply to author
Forward
0 new messages