更新时间:2016年11月17日16时01分 来源:传智播客web前端开发培训学院 浏览次数:
| 1 2 3 | // bad (albeit way faster)const arr = [1, 2, 3, 4];const len = arr.length;var i = -1;var result = [];while (++i < len) {  var n = arr;  if (n % 2 > 0) continue;  result.push(n * n);}// goodconst arr = [1, 2, 3, 4];const isEven = n => n % 2 == 0;const square = n => n * n;const result = arr.filter(isEven).map(square); | 
| 1 2 3 4 5 | // badconst merge = (target, ...sources) => Object.assign(target, ...sources);merge({ foo: "foo"}, { bar: "bar"}); // => { foo: "foo", bar: "bar" }// goodconst merge = (...sources) => Object.assign({}, ...sources);merge({ foo: "foo"}, { bar: "bar"}); // => { foo: "foo", bar: "bar" } | 
| 1 2 | // badconst toArray = obj => [].slice.call(obj);// goodconst toArray = (() =>  Array.from ? Array.from : obj => [].slice.call(obj))(); | 
| 1 | // badif (x === undefined || x === null) { ... }// goodif (x == undefined) { ... } | 
| 1 2 3 4 5 6 7 8 9 | // badconst sum = arr => {  var sum = 0;  var i = -1;  for (;arr[++i];) {    sum += arr;  }  returnsum;};sum([1, 2, 3]); // => 6// goodconst sum = arr =>  arr.reduce((x, y) => x + y);sum([1, 2, 3]); // => 6 | 
| 01 02 03 04 05 06 07 08 09 10 11 12 | // badconst createDivs = howMany => {  while (howMany--) {    document.body.insertAdjacentHTML("beforeend", "<div></div>");  }};createDivs(5);// badconst createDivs = howMany =>  [...Array(howMany)].forEach(() =>    document.body.insertAdjacentHTML("beforeend", "<div></div>")  );createDivs(5);// goodconst createDivs = howMany => {  if (!howMany) return;  document.body.insertAdjacentHTML("beforeend", "<div></div>");  returncreateDivs(howMany - 1);};createDivs(5); | 
| 1 | // badconst sortNumbers = () =>  Array.prototype.slice.call(arguments).sort();// goodconst sortNumbers = (...numbers) => numbers.sort(); | 
| 1 | const greet = (first, last) => `Hi ${first} ${last}`;const person = ["John", "Doe"];// badgreet.apply(null, person);// goodgreet(...person); | 
| 1 | // bad["foo", "bar"].forEach(func.bind(this));// good["foo", "bar"].forEach(func, this); | 
| 01 02 03 04 05 06 07 08 09 10 11 12 | // badconst person = {  first: "John",  last: "Doe",  greet() {    const full = function() {      return`${this.first} ${this.last}`;    }.bind(this);    return`Hello ${full()}`;  }}// goodconst person = {  first: "John",  last: "Doe",  greet() {    const full = () => `${this.first} ${this.last}`;    return`Hello ${full()}`;  }} | 
| 1 | // bad[1, 2, 3].map(num => String(num));// good[1, 2, 3].map(String); | 
| 1 2 | const plus1 = a => a + 1;const mult2 = a => a * 2;// badmult2(plus1(5)); // => 12// goodconst pipeline = (...funcs) => val => funcs.reduce((a, b) => b(a), val);const addThenMult = pipeline(plus1, mult2);addThenMult(5); // => 12 | 
| 1 2 3 4 5 6 7 8 | // badconst contains = (arr, value) =>  Array.prototype.includes    ? arr.includes(value)    : arr.some(el => el === value);contains(["foo", "bar"], "baz"); // => false// goodconst contains = (() =>  Array.prototype.includes    ? (arr, value) => arr.includes(value)    : (arr, value) => arr.some(el => el === value))();contains(["foo", "bar"], "baz"); // => false | 
| 1 2 3 | // badvar me = new Map();me.set("name", "Ben").set("country", "Belgium");// goodconst me = new Map();me.set("name", "Ben").set("country", "Belgium"); | 
| 1 2 3 4 5 | // badvar grade;if (result < 50)  grade = "bad";elseif(result < 90)  grade = "good";else  grade = "excellent";// goodconst grade = (() => {  if (result < 50)    return "bad";  if (result < 90)    return "good";  return "excellent";})(); | 
| 1 2 3 4 5 6 7 | const shared = { foo: "foo"};const obj = Object.create(shared, {  bar: {    value: "bar",    enumerable: true  }});// badfor (var prop in obj) {  if (obj.hasOwnProperty(prop))    console.log(prop);}// goodObject.keys(obj).forEach(prop => console.log(prop)); | 
| 01 02 03 04 05 06 07 08 09 10 | // badconst me = {  name: "Ben",  age: 30};varmeSize = Object.keys(me).length;meSize; // => 2me.country = "Belgium";meSize++;meSize; // => 3// goodconst me = new Map();me.set("name", "Ben");me.set("age", 30);me.size; // => 2me.set("country", "Belgium");me.size; // => 3 | 
| 1 2 3 | // badconst sum = a => b => a + b;sum(5)(3); // => 8// goodconst sum = (a, b) => a + b;sum(5, 3); // => 8 | 
| 1 | // badfoo || doSomething();// goodif (!foo) doSomething(); | 
| 1 | // badvoid function() { /* IIFE */ }();// good(function() { /* IIFE */ }()); | 
| 1 | // badconst n = ~~3.14;// goodconst n = Math.floor(3.14); | 
| 1 2 | // badarr[arr.length - 1];// goodconst first = arr => arr[0];const last = arr => first(arr.slice(-1));last(arr); | 
| 1 | // badconst product = (a, b) => a * b;const triple = n => n * 3;// goodconst product = (a, b) => a * b;const triple = product.bind(null, 3); | 
| 1 2 3 4 5 6 7 8 | // badvar _ = require("underscore");_.compact(["foo", 0]));_.unique(["foo", "foo"]);_.union(["foo"], ["bar"], ["foo"]);// goodconst compact = arr => arr.filter(el => el);const unique = arr => [...Set(arr)];const union = (...arr) => unique([].concat(...arr));compact(["foo", 0]);unique(["foo", "foo"]);union(["foo"], ["bar"], ["foo"]); | 
