callback) { let pattern = route.path || ''; // scope is same as var if (pattern.indexOf('/') !== 0) { // Relative paths build on the parent's path. pattern = basename.replace(/\/*$/, '/') + pattern; } // and so on }
let state = initialState; // scope is same as var let dispatching = false; // scope is same as var function dispatch(action) { // store.dispatch(action) if (dispatching) { throw new Error('Reducers may not…’); } try { dispatching = true; state = reducer(state, action); } finally { dispatching = false; }
by factor. function scale(factor, point) { let scaled = []; // scope is same as var for (let i = 0, length = point.length; i < length; i += 1) { scaled[i] = factor * point[i]; } // scope of i and length is limited to the for statement return scaled; } //But map provides a functional alternative without let! //return point.map(function (coord) { return factor * coord; });
mongoClient = new MongoClient(…); // scope is similar to var mongoClient.open(function (err, client) { if (err) { console.error(err); } else { let db = client.db(dbName); if (db) { … } } // scope of db is limited to the else block }); //But connect provides a functional alternative without let! MongoClient.connect(…, function (err, db) { … });
tight cohesion, then var was the old let :) Even if function scope is your scope, traditional variable scope matters to traditional software developers…
7; function area(r) { return PI * r * r; } // Error in strict mode; some engines ignore it in non-strict mode :( PI = 3.141592; // You don’t reassign it in this program: const areaOfMyCircle = area(7);
the object // which was assigned to the const variable. action.type = ADD_TODO; action.text = 'Understand ES2015'; // Error in strict mode: you can’t reassign the const variable. action = { type: ADD_TODO, text: 'Understand ES2015' };
// Given the previous state and an action, return the next state. function reduceTodos(todos, {type, text}) { // ES5: action switch (type) { // ES5: action.type case ADD_TODO: return todos.concat({ text, // ES5: text: action.text completed: false }); default: return todos; } }
import {Router, Route, Link} from 'react-router'; // ES5 CommonJS require // var ReactRouter = require('react-router'); // var Router = ReactRouter.Router; // var Route = ReactRouter.Route; // var Link = ReactRouter.Link;
{filter, todos} from '../reducers'; const store = createStore(combineReducers({ filter, todos })); // var Redux = require('redux'); // var reducers = require('../reducers'); // var store = Redux.createStore(Redux.combineReducers({ // filter: reducers.filter, // todos: reducers.todos // });
{filter, todos} from '../reducers'; const store = createStore(combineReducers({filter, todos})); // ES2015 namespace import: * represents the named exports object import {createStore, combineReducers} from 'redux'; import * as reducers from '../reducers'; const store = createStore(combineReducers(reducers));
* r; const area = r => Math.PI * r * r; // Parentheses are optional if a function has one argument. // const diameter = (r) => 2 * r; // const area = (r) => Math.PI * r * r; // ES5 functions // var diameter = function (r) { return 2 * r; }; // var area = function (r) { return Math.PI * r * r; }; // function diameter(r) { return 2 * r; } // function area(r) { return Math.PI * r * r; }
diameter = r => 2 * r; export const area = r => Math.PI * r * r; // ES2015 arrow functions as methods of an object // For example, an ES5 CommonJS exports object exports = { diameter: r => 2 * r, area: r => Math.PI * r * r };
by factor. // ES2015 arrow function argument of map function function scale(factor, point) { return point.map(coord => factor * coord); } // ES5 function argument of map function function scale(factor, point) { return point.map(function (coord) { return factor * coord; }); }
by factor. // ES2015 arrow function only for argument of map function function scale(factor, point) { return point.map(coord => factor * coord); } // ES2015 arrow functions for scale and argument of map function // Parentheses are required if it has zero or multiple arguments. const scale = (factor, point) => point.map(coord => factor * coord); // Here is a way you might describe the arrow functions: // “Given a factor and a point, return a new point array // in which each coordinate is scaled by factor.”