那里有一个很好的JS速记参考吗?

Isa*_*bow 29 javascript shorthand

我想在我的常规编码习惯中加入任何速记技术,并且当我在压缩代码中看到它们时也能够阅读它们.

有人知道概述技术的参考页面或文档吗?

编辑:我之前提到过minifiers,现在我很清楚,缩小和高效的JS输入技术是两个几乎完全不同的概念.

gbl*_*zex 42

更新ECMAScript 2015(ES6)的好东西.见底部.

最常见的条件短线是:

a = a || b     // if a is falsy use b as default
a || (a = b)   // another version of assigning a default value
a = b ? c : d  // if b then c else d
a != null      // same as: (a !== null && a !== undefined) , but `a` has to be defined
Run Code Online (Sandbox Code Playgroud)

用于创建对象和数组的对象文字表示法:

obj = {
   prop1: 5,
   prop2: function () { ... },
   ...
}
arr = [1, 2, 3, "four", ...]

a = {}     // instead of new Object()
b = []     // instead of new Array()
c = /.../  // instead of new RegExp()
Run Code Online (Sandbox Code Playgroud)

内置类型(数字,字符串,日期,布尔值)

// Increment/Decrement/Multiply/Divide
a += 5  // same as: a = a + 5
a++     // same as: a = a + 1

// Number and Date
a = 15e4        // 150000
a = ~~b         // Math.floor(b) if b is always positive
a = +new Date   // new Date().getTime()

// toString, toNumber, toBoolean
a = +"5"        // a will be the number five (toNumber)
a = "" + 5 + 6  // "56" (toString)
a = !!"exists"  // true (toBoolean)
Run Code Online (Sandbox Code Playgroud)

变量声明:

var a, b, c // instead of var a; var b; var c;
Run Code Online (Sandbox Code Playgroud)

索引处的字符串字符:

"some text"[1] // instead of "some text".charAt(1);
Run Code Online (Sandbox Code Playgroud)

ECMAScript 2015(ES6)标准简介

这些是相对较新的补充,所以不要指望浏览器之间的广泛支持.它们可能受现代环境(例如:较新的node.js)或通过转发器支持."旧"版本当然会继续工作.

箭头功能

a.map(s => s.length)                    // new
a.map(function(s) { return s.length })  // old
Run Code Online (Sandbox Code Playgroud)

休息参数

// new 
function(a, b, ...args) {
  // ... use args as an array
}

// old
function f(a, b){
  var args = Array.prototype.slice.call(arguments, f.length)
  // ... use args as an array
}
Run Code Online (Sandbox Code Playgroud)

默认参数值

function f(a, opts={}) { ... }                   // new
function f(a, opts) { opts = opts || {}; ... }   // old
Run Code Online (Sandbox Code Playgroud)

解构

var bag = [1, 2, 3]
var [a, b, c] = bag                     // new  
var a = bag[0], b = bag[1], c = bag[2]  // old  
Run Code Online (Sandbox Code Playgroud)

对象文字内的方法定义

// new                  |        // old
var obj = {             |        var obj = {
    method() { ... }    |            method: function() { ... }
};                      |        };
Run Code Online (Sandbox Code Playgroud)

对象文字内的计算属性名称

// new                               |      // old
var obj = {                          |      var obj = { 
    key1: 1,                         |          key1: 5  
    ['key' + 2]() { return 42 }      |      };
};                                   |      obj['key' + 2] = function () { return 42 } 
Run Code Online (Sandbox Code Playgroud)

奖励:内置对象的新方法

// convert from array-like to real array
Array.from(document.querySelectorAll('*'))                   // new
Array.prototype.slice.call(document.querySelectorAll('*'))   // old

'crazy'.includes('az')         // new
'crazy'.indexOf('az') != -1    // old

'crazy'.startsWith('cr')       // new (there's also endsWith)
'crazy'.indexOf('az') == 0     // old

'*'.repeat(n)                  // new
Array(n+1).join('*')           // old 
Run Code Online (Sandbox Code Playgroud)

奖励2:箭头功能也使得self = this捕获不必要

// new (notice the arrow)
function Timer(){
    this.state = 0;
    setInterval(() => this.state++, 1000); // `this` properly refers to our timer
}

// old
function Timer() {
    var self = this; // needed to save a reference to capture `this`
    self.state = 0;
    setInterval(function () { self.state++ }, 1000); // used captured value in functions
}
Run Code Online (Sandbox Code Playgroud)

  • `x &&(doWhentrue);`<<条件.性能与`if(x)do;`相同 (6认同)

Dan*_*llo 17

如果通过JavaScript,您还包含比版本1.5更新的版本,那么您还可以看到以下内容:


表达式闭包:

JavaScript 1.7及更早版本:

var square = function(x) { return x * x; }
Run Code Online (Sandbox Code Playgroud)

JavaScript 1.8添加了一个简写的Lambda表示法,用于编写带有表达式闭包的简单函数:

var square = function(x) x * x;
Run Code Online (Sandbox Code Playgroud)

reduce()方法:

JavaScript 1.8还向数组引入了reduce()方法:

var total = [0, 1, 2, 3].reduce(function(a, b){ return a + b; });  
// total == 6 
Run Code Online (Sandbox Code Playgroud)

解构分配:

在JavaScript 1.7中,您可以使用解构赋值来交换值以避免临时变量:

var a = 1;  
var b = 3;  

[a, b] = [b, a]; 
Run Code Online (Sandbox Code Playgroud)

Array Comprehensions和filter()方法:

在JavaScript 1.7中引入了Array Comprehensions,它可以减少以下代码:

var numbers = [1, 2, 3, 21, 22, 30];  
var evens = [];

for (var i = 0; i < numbers.length; i++) {
  if (numbers[i] % 2 === 0) {
    evens.push(numbers[i]);
  }
}
Run Code Online (Sandbox Code Playgroud)

对于这样的事情:

var numbers = [1, 2, 3, 21, 22, 30];
var evens = [i for each(i in numbers) if (i % 2 === 0)];
Run Code Online (Sandbox Code Playgroud)

或者使用filter()JavaScript 1.6中引入的Arrays中的方法:

var numbers = [1, 2, 3, 21, 22, 30];
var evens = numbers.filter(function(i) { return i % 2 === 0; });  
Run Code Online (Sandbox Code Playgroud)