Skip to content

Instantly share code, notes, and snippets.

@Ado77
Forked from ericelliott/defaults-overrides.md
Created September 7, 2018 14:07
Show Gist options
  • Select an option

  • Save Ado77/38ecabd853bd7a1fcacad2bdb9311147 to your computer and use it in GitHub Desktop.

Select an option

Save Ado77/38ecabd853bd7a1fcacad2bdb9311147 to your computer and use it in GitHub Desktop.

Revisions

  1. @ericelliott ericelliott revised this gist May 1, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions defaults-overrides.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ function foo ({
    baz = 'works!'
    } = {}) {

    return(`${bar}, ${baz}`);
    return (`${bar}, ${baz}`);
    }

    console.log(foo({
    @@ -33,7 +33,7 @@ function foo2 (options) {
    bar = settings.bar,
    baz = settings.baz;

    return(bar + ', ' +baz);
    return (bar + ', ' +baz);
    }

    console.log(foo2({
  2. @ericelliott ericelliott revised this gist May 1, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions defaults-overrides.md
    Original 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
  3. @ericelliott ericelliott revised this gist May 1, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion defaults-overrides.md
    Original 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)
    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)
  4. @ericelliott ericelliott revised this gist May 1, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion defaults-overrides.md
    Original file line number Diff line number Diff line change
    @@ -39,4 +39,4 @@ console.log(foo2({
    })); // 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)
    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)
  5. @ericelliott ericelliott renamed this gist May 1, 2015. 1 changed file with 10 additions and 7 deletions.
    17 changes: 10 additions & 7 deletions defaults-overrides.js → defaults-overrides.md
    Original 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.
    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:
    Equivalent to ES5:

    // This bit needs to be polyfilled,
    // Or use $.extend(), _.extend(),
    // lodash/object/assign aka _.assign()
    // or equivalent.
    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)
  6. @ericelliott ericelliott revised this gist Apr 28, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions defaults-overrides.js
    Original 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!'
  7. @ericelliott ericelliott revised this gist Apr 24, 2015. 1 changed file with 6 additions and 38 deletions.
    44 changes: 6 additions & 38 deletions defaults-overrides.js
    Original file line number Diff line number Diff line change
    @@ -1,43 +1,11 @@
    // The defaults / overrides pattern in ES6
    // Combine default parameters and destructuring for a compact
    // version of the defaults / overrides pattern.

    // This *looks like* it should work, but
    // prints, "yay, undefined"
    function foo ({
    bar = 'no',
    baz = 'works!'
    } = {}) {

    // 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
    // *completely replaced* by whatever you
    // pass in.
    /*
    let defaults = {
    bar: 'no',
    baz: 'works!'
    };
    function foo ({ bar, baz } = defaults) {
    return(`${bar}, ${baz}`);
    }
    console.log(foo({
    bar: 'yay'
    }));
    */

    // So you really have to do this:
    let defaults = {
    bar: 'no',
    baz: 'works!'
    };

    let defaults = {
    bar: 'no',
    baz: 'works!'
    };

    function foo (options) {
    let { bar, baz } = Object.assign({}, defaults, options);
    return(`${bar}, ${baz}`);
    }

  8. @ericelliott ericelliott revised this gist Apr 19, 2015. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions defaults-overrides.js
    Original 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 settings = Object.assign({}, defaults, options);
    let { bar, baz } = settings;
    let { bar, baz } = Object.assign({}, defaults, options);
    return(`${bar}, ${baz}`);
    }

  9. @ericelliott ericelliott revised this gist Apr 19, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion defaults-overrides.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    // prints, "yay, undefined"

    // Why? Because what you're destructuring
    // in that assignment is the entire arguments
    // 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
  10. @ericelliott ericelliott revised this gist Apr 19, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions defaults-overrides.js
    Original 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.
    // *anything*, your defaults object gets
    // *completely replaced* by whatever you
    // pass in.
    /*
    let defaults = {
    bar: 'no',
  11. @ericelliott ericelliott revised this gist Apr 19, 2015. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions defaults-overrides.js
    Original 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',
  12. @ericelliott ericelliott revised this gist Apr 19, 2015. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions defaults-overrides.js
    Original file line number Diff line number Diff line change
    @@ -47,11 +47,11 @@ var defaults2 = {
    };

    function foo2 (options) {
    var settings = Object.assign({}, defaults2, options),
    bar = options.bar,
    baz = options.baz;
    var settings = assign({}, defaults2, options),
    bar = settings.bar,
    baz = settings.baz;

    return(bar + ', + ' baz);
    return(bar + ', ' +baz);
    }

    console.log(foo2({
  13. @ericelliott ericelliott revised this gist Apr 19, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion defaults-overrides.js
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,8 @@ let defaults = {
    };

    function foo (options) {
    { bar, baz } = Object.assign({}, defaults, options);
    let settings = Object.assign({}, defaults, options);
    let { bar, baz } = settings;
    return(`${bar}, ${baz}`);
    }

  14. @ericelliott ericelliott revised this gist Apr 19, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions defaults-overrides.js
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ function foo ({ bar, baz } = defaults) {
    }
    console.log(foo({
    bar: 'yay',
    bar: 'yay'
    }));
    */

    @@ -29,7 +29,7 @@ function foo (options) {
    }

    console.log(foo({
    bar: 'yay',
    bar: 'yay'
    }));

    // Equivalent to ES5:
  15. @ericelliott ericelliott revised this gist Apr 19, 2015. 1 changed file with 26 additions and 4 deletions.
    30 changes: 26 additions & 4 deletions defaults-overrides.js
    Original 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',
    baz: 'works!'}));
    }));
    */

    // 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);
    var settings = Object.assign({}, defaults2, options),
    bar = options.bar,
    baz = options.baz;

    return(bar + ', + ' baz);
    }

    console.log(foo2({
    bar: 'yay',
    baz: 'works!'}));
    bar: 'yay'
    }));
  16. @ericelliott ericelliott created this gist Apr 19, 2015.
    36 changes: 36 additions & 0 deletions defaults-overrides.js
    Original 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!'}));