-
-
Save Ado77/38ecabd853bd7a1fcacad2bdb9311147 to your computer and use it in GitHub Desktop.
Revisions
-
ericelliott revised this gist
May 1, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ function foo ({ baz = 'works!' } = {}) { return (`${bar}, ${baz}`); } console.log(foo({ @@ -33,7 +33,7 @@ function foo2 (options) { bar = settings.bar, baz = settings.baz; return (bar + ', ' +baz); } console.log(foo2({ -
ericelliott revised this gist
May 1, 2015 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ # ES6 Defaults / Overrides Pattern Combine default parameters and destructuring for a compact version of the defaults / overrides pattern. ```js -
ericelliott revised this gist
May 1, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -39,4 +39,4 @@ console.log(foo2({ })); // logs 'yay, works!' ``` Gist written for [How to Use ES6 for Isomorphic JavaScript Apps](https://medium.com/javascript-scene/how-to-use-es6-for-isomorphic-javascript-apps-2a9c3abe5ea2) -
ericelliott revised this gist
May 1, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -39,4 +39,4 @@ console.log(foo2({ })); // logs 'yay, works!' ``` Gist written for [how to use ES6 for isomorphic JavaScript Apps](https://medium.com/javascript-scene/how-to-use-es6-for-isomorphic-javascript-apps-2a9c3abe5ea2) -
ericelliott renamed this gist
May 1, 2015 . 1 changed file with 10 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ Combine default parameters and destructuring for a compact version of the defaults / overrides pattern. ```js function foo ({ bar = 'no', baz = 'works!' @@ -12,13 +12,13 @@ function foo ({ console.log(foo({ bar: 'yay' })); // logs 'yay, works!' ``` Equivalent to ES5: This bit needs to be polyfilled, Or use $.extend(), _.extend(), lodash/object/assign aka _.assign() or equivalent. ```js var assign = Object.assign; var defaults2 = { @@ -37,3 +37,6 @@ function foo2 (options) { console.log(foo2({ bar: 'yay' })); // logs 'yay, works!' ``` Learn [how to use ES6 for isomorphic JavaScript Apps](https://medium.com/javascript-scene/how-to-use-es6-for-isomorphic-javascript-apps-2a9c3abe5ea2) -
ericelliott revised this gist
Apr 28, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,7 @@ function foo ({ console.log(foo({ bar: 'yay' })); // logs 'yay, works!' // Equivalent to ES5: @@ -36,4 +36,4 @@ function foo2 (options) { console.log(foo2({ bar: 'yay' })); // logs 'yay, works!' -
ericelliott revised this gist
Apr 24, 2015 . 1 changed file with 6 additions and 38 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,43 +1,11 @@ // Combine default parameters and destructuring for a compact // version of the defaults / overrides pattern. function foo ({ bar = 'no', baz = 'works!' } = {}) { return(`${bar}, ${baz}`); } -
ericelliott revised this gist
Apr 19, 2015 . 1 changed file with 6 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,9 +31,13 @@ let defaults = { baz: 'works!' }; let defaults = { bar: 'no', baz: 'works!' }; function foo (options) { let { bar, baz } = Object.assign({}, defaults, options); return(`${bar}, ${baz}`); } -
ericelliott revised this gist
Apr 19, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ // prints, "yay, undefined" // Why? Because what you're destructuring // in the assignment is the entire arguments // collection. If nothing is passed, it // loads the defaults, but if you pass // *anything*, your defaults object gets -
ericelliott revised this gist
Apr 19, 2015 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,9 +7,9 @@ // in that assignment is the entire arguments // collection. If nothing is passed, it // loads the defaults, but if you pass // *anything*, your defaults object gets // *completely replaced* by whatever you // pass in. /* let defaults = { bar: 'no', -
ericelliott revised this gist
Apr 19, 2015 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,6 +2,14 @@ // This *looks like* it should work, but // prints, "yay, undefined" // Why? Because what you're destructuring // in that assignment is the entire arguments // collection. If nothing is passed, it // loads the defaults, but if you pass // *anything*, your defaults object // gets *completely replaced* by // whatever you pass in. /* let defaults = { bar: 'no', -
ericelliott revised this gist
Apr 19, 2015 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -47,11 +47,11 @@ var defaults2 = { }; function foo2 (options) { var settings = assign({}, defaults2, options), bar = settings.bar, baz = settings.baz; return(bar + ', ' +baz); } console.log(foo2({ -
ericelliott revised this gist
Apr 19, 2015 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,7 +24,8 @@ let defaults = { }; function foo (options) { let settings = Object.assign({}, defaults, options); let { bar, baz } = settings; return(`${bar}, ${baz}`); } -
ericelliott revised this gist
Apr 19, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,7 @@ function foo ({ bar, baz } = defaults) { } console.log(foo({ bar: 'yay' })); */ @@ -29,7 +29,7 @@ function foo (options) { } console.log(foo({ bar: 'yay' })); // Equivalent to ES5: -
ericelliott revised this gist
Apr 19, 2015 . 1 changed file with 26 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,8 @@ // The defaults / overrides pattern in ES6 // This *looks like* it should work, but // prints, "yay, undefined" /* let defaults = { bar: 'no', baz: 'works!' @@ -11,8 +14,23 @@ function foo ({ bar, baz } = defaults) { console.log(foo({ bar: 'yay', })); */ // So you really have to do this: let defaults = { bar: 'no', baz: 'works!' }; function foo (options) { { bar, baz } = Object.assign({}, defaults, options); return(`${bar}, ${baz}`); } console.log(foo({ bar: 'yay', })); // Equivalent to ES5: @@ -28,9 +46,13 @@ var defaults2 = { }; function foo2 (options) { var settings = Object.assign({}, defaults2, options), bar = options.bar, baz = options.baz; return(bar + ', + ' baz); } console.log(foo2({ bar: 'yay' })); -
ericelliott created this gist
Apr 19, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ // The defaults / overrides pattern in ES6 let defaults = { bar: 'no', baz: 'works!' }; function foo ({ bar, baz } = defaults) { return(`${bar}, ${baz}`); } console.log(foo({ bar: 'yay', baz: 'works!'})); // Equivalent to ES5: // This bit needs to be polyfilled, // Or use $.extend(), _.extend(), // lodash/object/assign aka _.assign() // or equivalent. var assign = Object.assign; var defaults2 = { bar: 'no', baz: 'works!' }; function foo2 (options) { var settings = Object.assign({}, defaults2, options); } console.log(foo2({ bar: 'yay', baz: 'works!'}));