Skip to content

Instantly share code, notes, and snippets.

@pha-ias
Last active August 23, 2016 19:22
Show Gist options
  • Select an option

  • Save pha-ias/57dd71eaeb6d2e43e531e02617129f9a to your computer and use it in GitHub Desktop.

Select an option

Save pha-ias/57dd71eaeb6d2e43e531e02617129f9a to your computer and use it in GitHub Desktop.

Revisions

  1. pha-ias revised this gist Aug 23, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ES6-beyond Template Literals, Arrow Functions
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    // ES6 and beyond

    // -------------------------
    //String Literal
    // String Literal
    // -------------------------

    var someString = `Something`
  2. pha-ias created this gist Aug 23, 2016.
    132 changes: 132 additions & 0 deletions ES6-beyond Template Literals, Arrow Functions
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,132 @@
    // ES6 and beyond

    // -------------------------
    //String Literal
    // -------------------------

    var someString = `Something`
    typeof someString

    // Multiline support
    var someText =
    `
    Something really
    long
    and not interesting
    `
    var desc = "awesome"

    // Exception when declaring a string?!
    var someText =
    `
    Something ${awesome}
    long
    and not interesting
    `;
    // VM446:3 Uncaught ReferenceError: awesome is not defined(…)(anonymous function) @ VM446:3

    var someText =
    `
    Something ${desc}
    long
    and not interesting
    `
    ;

    var someText =
    `
    Something ${desc.toUpperCase()}
    long
    and not interesting
    `
    ;
    someText

    function upper(str) { return str.toUpperCase(); }

    var someText =
    `
    Something ${upper(desc)}
    long
    and not interesting
    `
    ;
    someText

    desc = "sucky";
    someText

    var someGlobal = 'some global';
    function getGlobal() { return someGlobal; }

    var someText =
    `
    Something ${getGlobal()}
    long
    and not interesting
    `
    ;

    someText

    someGlobal = 'still a global'
    getGlobal()
    someText

    function foo(str) {
    var name = 'foo';
    console.log(str)
    }

    function bar() {
    var name = 'bar';
    foo(`Hello from ${name}`);
    }

    var name = 'initial';
    bar()

    var someText = `Some text has: ${upper(`${desc}`)}`;

    // -------------------------
    // Tagged String Literal
    // -------------------------
    function foo(strings, ...values) {
    console.log( strings );
    console.log( values );
    }

    var desc = "awesome";

    foo`Everything is ${desc}!`;
    foo `Everything is ${desc}!`;
    foo `Everything is ${desc}!`;

    foo`Everything is ${desc} and ${desc2}!`;
    foo(`Everything is ${desc} and ${desc2}!`); // function?
    foo`${desc} and ${desc2}`;

    String.raw`Hello`
    String.raw`Hello` === "Hello"
    String.raw`Hello\n\n${desc}`

    String.raw`
    Hello\n\n${desc}`

    // -------------------------
    // Arrow Functions
    // -------------------------

    var neg = x => -x
    neg(234)

    [1, 3, 5]

    a = [2, 4, 6, 8, 10]

    a.filter( e => e % 2)

    a = a.map( v => v * 2 );
    a = a.map( v => { return v * 2 });

    // [8, 16, 24, 32, 40]