// 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
// function divWrapper( element, index, arr ) { // array[index] = '<' + this.tag + '>' // array[index] += element // array[index] += '' // ES6 Template Literal // array[index] = `<${this.tag} class="${this.class}">${element}`; // } // Call forEach with and pass in thisDiv as Argument // fruits.forEach( divWrapper, divElementObj ) //----------------------------------------- // Passing in an anonymous function as first argument fruits.forEach( function( element, index, array ) { array[index] = '<' + this.tag + '>' array[index] += element array[index] += '' // ES6 Template Literal // return `<${this.tag} class="${this.class}">${element}`; }, divElementObj ) // After forEach was called on fruits Array. console.log( newFruits )