From e1e1aec680df4e1513379bd8160014f84e9cbff5 Mon Sep 17 00:00:00 2001 From: manuk Date: Wed, 16 Jan 2019 23:04:04 +0000 Subject: [PATCH] ES6 styleguide: Allow usage of destructuring assignment. Change-Id: I7d710dc1964ac10bcca6a71d9f45c21e8fbbf96e Reviewed-on: https://chromium-review.googlesource.com/c/1398390 Commit-Queue: manuk hovanesian Reviewed-by: Dan Beam Cr-Commit-Position: refs/heads/master@{#623427} --- .../site_settings_page/site_settings_page.js | 4 +- styleguide/web/es.md | 89 +++++++++++-------- 2 files changed, 52 insertions(+), 41 deletions(-) diff --git a/chrome/browser/resources/settings/site_settings_page/site_settings_page.js b/chrome/browser/resources/settings/site_settings_page/site_settings_page.js index 90d94575a35a98..685ad1a6e1f50d 100644 --- a/chrome/browser/resources/settings/site_settings_page/site_settings_page.js +++ b/chrome/browser/resources/settings/site_settings_page/site_settings_page.js @@ -134,9 +134,7 @@ Polymer({ pairs.push([R.SITE_SETTINGS_PAYMENT_HANDLER, 'paymentHandler']); } - pairs.forEach(pair => { - const route = pair[0]; - const id = pair[1]; + pairs.forEach(([route, id]) => { this.focusConfig.set(route.path, () => this.async(() => { cr.ui.focusWithoutInk(assert(this.$$(`#${id}`))); })); diff --git a/styleguide/web/es.md b/styleguide/web/es.md index b0dcd23a0d62fb..d4db948aadbf92 100644 --- a/styleguide/web/es.md +++ b/styleguide/web/es.md @@ -511,6 +511,57 @@ usesRestParams('a', 'b', 1, 2, 3); --- +### Destructuring Assignment + +Flexible destructuring of collections or parameters. + +**Usage Example:** + +```js +// Array +const [a, , b] = [1, 2, 3]; // a = 1, b = 3 + +// Object +const {width, height} = document.body.getBoundingClientRect(); +// width = rect.width, height = rect.height + +// Parameters +function f([name, val]) { + console.log(name, val); // 'bar', 42 +} +f(['bar', 42, 'extra 1', 'extra 2']); // 'extra 1' and 'extra 2' are ignored. + +function g({name: n, val: v}) { + console.log(n, v); // 'foo', 7 +} +g({name: 'foo', val: 7}); + +function h({name, val}) { + console.log(name, val); // 'bar', 42 +} +h({name: 'bar', val: 42}); + +``` +**Mixing with [Rest Parameters](#rest-parameters)** + +Using rest parameters while destructuring objects is not supported by iOS 10 and requires setting the closure arg `language_in` to `ECMASCRIPT_2018`. + +```js +const {one, ...rest} = {one: 1, two: 2, three: 3}; +``` + +Using rest parameters while destructuring arrays, on the other hand, is supported by iOS 10 and `ECMASCRIPT_2017`. + +```js +const [one, ...rest] = [1, 2, 3]; +``` + +**Documentation:** [link](http://www.ecma-international.org/ecma-262/6.0/#sec-destructuring-assignment) + +**Discussion Notes / Link to Thread:** [link](https://groups.google.com/a/chromium.org/d/topic/chromium-dev/mwFnj7MTzgU) + +--- + ## Banned Features The following features are banned for Chromium development. @@ -680,44 +731,6 @@ result === 'yy' && re.lastIndex === 5; // true --- -### Destructuring Assignment - -Flexible destructuring of collections or parameters. - -**Usage Example:** - -```js -// Array -const [a, , b] = [1, 2, 3]; // a = 1, b = 3 - -// Object -const {width, height} = document.body.getBoundingClientRect(); -// width = rect.width, height = rect.height - -// Parameters -function f([name, val]) { - console.log(name, val); // 'bar', 42 -} -f(['bar', 42, 'extra 1', 'extra 2']); // 'extra 1' and 'extra 2' are ignored. - -function g({name: n, val: v}) { - console.log(n, v); // 'foo', 7 -} -g({name: 'foo', val: 7}); - -function h({name, val}) { - console.log(name, val); // 'bar', 42 -} -h({name: 'bar', val: 42}); - -``` - -**Documentation:** [link](https://tc39.github.io/ecma262/#sec-destructuring-assignment) - -**Discussion Notes / Link to Thread:** - ---- - ### Modules Support for exporting/importing values from/to modules without global