Skip to content

Instantly share code, notes, and snippets.

@marklearst
Last active January 8, 2017 03:52
Show Gist options
  • Select an option

  • Save marklearst/de5fc69efff14e9c9c0041898f817849 to your computer and use it in GitHub Desktop.

Select an option

Save marklearst/de5fc69efff14e9c9c0041898f817849 to your computer and use it in GitHub Desktop.

Revisions

  1. marklearst renamed this gist Jan 8, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .js → example.js
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@ console.log( '\n' );

    //-----------------------------------------
    // Passing in an anonymous function as first argument
    var newFruits = fruits.forEach( function( element, index, array ) {
    fruits.forEach( function( element, index, array ) {
    array[index] = '<' + this.tag + '>'
    array[index] += element
    array[index] += '</' + this.tag + '>'
  2. marklearst created this gist Jan 8, 2017.
    41 changes: 41 additions & 0 deletions .js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    // Object with div element and base attributes
    var divElementObj = {
    tag: 'div',
    class: 'col-4'
    }

    // An Array of Fruits
    var fruits = [
    'apple',
    'pear',
    'fig'
    ]

    // Logging fruits before forEach is called
    console.log( fruits );
    console.log( '\n' );

    //-----------------------------------------
    // Utility function that wraps text with a <div>
    // function divWrapper( element, index, arr ) {
    // array[index] = '<' + this.tag + '>'
    // array[index] += element
    // array[index] += '</' + this.tag + '>'
    // ES6 Template Literal
    // array[index] = `<${this.tag} class="${this.class}">${element}</${this.tag}>`;
    // }
    // Call forEach with and pass in thisDiv as Argument
    // fruits.forEach( divWrapper, divElementObj )

    //-----------------------------------------
    // Passing in an anonymous function as first argument
    var newFruits = fruits.forEach( function( element, index, array ) {
    array[index] = '<' + this.tag + '>'
    array[index] += element
    array[index] += '</' + this.tag + '>'
    // ES6 Template Literal
    // return `<${this.tag} class="${this.class}">${element}</${this.tag}>`;
    }, divElementObj )

    // After forEach was called on fruits Array.
    console.log( newFruits )