Benifits
Benifits
Benifits
(function () {
// We keep these variables private inside this closure scope
var myGrades = [93, 95, 88, 0, 55, 91];
var average = function() {
var total = myGrades.reduce(function(accumulator, item) {
return accumulator + item}, 0);
return 'Your average grade is ' + total / myGrades.length + '.';
}
var failing = function(){
var failingGrades = myGrades.filter(function(item) {
return item < 70;});
return 'You failed ' + failingGrades.length + ' times.';
}
console.log(failing());
}());
// ‘You failed 2 times.’
function myModule() {
this.hello = function() {
return 'hello!';
}
this.goodbye = function() {
return 'goodbye!';
}
}
module.exports = myModule;
var myModule = require('myModule');
var myModuleInstance = new myModule();
myModuleInstance.hello(); // 'hello!'
myModuleInstance.goodbye(); // 'goodbye!'
myModule.js
app.js
export let counter = 1;
export function increment() {
counter++;
}
export function decrement() {
counter--;
}
import * as counter from 'counter';
console.log(counter.counter); // 1
counter.increment();
console.log(counter.counter); // 2
counter.js
app.js
Browsers need one bundle (file) of ES5 code because of:
npm install webpack -g
webpack entrypoint.js app.bundle.js