# 正文

### 1. 三元运算符

```const x = 20;
if (x > 10) {
} else {
}```

`const answer = x > 10 ? 'is greater' : 'is lesser';`

`const big = x > 10 ? " greater 10" : x`

### 2. 简写短路求值

```if (variable1 !== null || variable1 !== undefined || variable1 !== '') {
let variable2 = variable1;
}```

`const variable2 = variable1  || 'new';`

### 3. 简写变量声明

```let x;
let y;
let z = 3;```

`let x, y, z=3;`

### 4. 简写 if 执行条件

`if (likeJavaScript === true)`

`if (likeJavaScript)`

```let a;
if ( a !== true ) {
// do something...
}```

```let a;
if ( !a ) {
// do something...
}```

### 5. 简写 JavaScript 循环方法

`for (let i = 0; i < allImgs.length; i++)`

`for (let index in allImgs)`

```function logArrayElements(element, index, array) {
console.log("a[" + index + "] = " + element);
}
[2, 5, 9].forEach(logArrayElements);
// logs:
// a[0] = 2
// a[1] = 5
// a[2] = 9```

### 6. 短路求值

```let dbHost;
if (process.env.DB_HOST) {
dbHost = process.env.DB_HOST;
} else {
dbHost = 'localhost';
}```

`const dbHost = process.env.DB_HOST || 'localhost';`

### 7. 十进制指数

`for (let i = 0; i < 10000; i++) {}`

```for (let i = 0; i < 1e7; i++) {}

// 下面都是返回 true
1e0 === 1;
1e1 === 10;
1e2 === 100;
1e3 === 1000;
1e4 === 10000;
1e5 === 100000;```

### 8. 简写对象属性

`const obj = { x:x, y:y };`

`const obj = { x, y };`

### 9. 简写箭头函数

```function sayHello(name) {
console.log('Hello', name);
}

setTimeout(function() {
}, 2000);

list.forEach(function(item) {
console.log(item);
});```

```sayHello = name => console.log('Hello', name);

list.forEach(item => console.log(item));```

### 10. 简写隐式返回值

```function calcCircumference(diameter) {
return Math.PI * diameter
}

var func = function func() {
return { foo: 1 };
};```

```calcCircumference = diameter => (
Math.PI * diameter;
)

var func = () => ({ foo: 1 });```

### 11. 默认参数值

```function volume(l, w, h) {
if (w === undefined)
w = 3;
if (h === undefined)
h = 4;
return l * w * h;
}```

```volume = (l, w = 3, h = 4 ) => (l * w * h);

volume(2)   // output: 24```

### 12. 字符串模板

```const welcome = 'You have logged in as ' + first + ' ' + last + '.'

const db = 'http://' + host + ':' + port + '/' + database;```

```const welcome = `You have logged in as \${first} \${last}`;

const db = `http://\${host}:\${port}/\${database}`;```

### 13. 简写赋值方法

```const observable = require('mobx/observable');
const action = require('mobx/action');
const runInAction = require('mobx/runInAction');

const store = this.props.store;
const form = this.props.form;
const errors = this.props.errors;
const entity = this.props.entity;```

```import { observable, action, runInAction } from 'mobx';

```// 最后一个变量名为 contact

### 14. 简写多行字符串

```const lorem = 'Lorem ipsum dolor sit amet, consectetur\n\t'
+ 'adipisicing elit, sed do eiusmod tempor incididunt\n\t'
+ 'ut labore et dolore magna aliqua. Ut enim ad minim\n\t'
+ 'veniam, quis nostrud exercitation ullamco laboris\n\t'
+ 'nisi ut aliquip ex ea commodo consequat. Duis aute\n\t'
+ 'irure dolor in reprehenderit in voluptate velit esse.\n\t'```

```const lorem = `Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse.````

### 15. 扩展运算符

`ES6`中，包括扩展运算符，它可以使你的操作更简单，例如：

```// joining arrays
const odd = [1, 3, 5];
const nums = [2 ,4 , 6].concat(odd);

// cloning arrays
const arr = [1, 2, 3, 4];
const arr2 = arr.slice()```

```// joining arrays
const odd = [1, 3, 5];
const nums = [2 ,4 , 6, ...odd];
console.log(nums);   // [2, 4, 6, 1, 3, 5]

// cloning arrays
const arr = [1, 2, 3, 4];
const arr2 = [...arr];```

```const odd = [1, 3, 5 ];
const nums = [2, ...odd, 4, 6];```

```const { a, b, ...z } = { a: 1, b: 2, c: 3, d: 4 };
console.log(a)   // 1
console.log(b)   // 2
console.log(z)   // { c: 3, d: 4 }```

### 16. 强制参数

```function foo(bar) {
if(bar === undefined) {
throw new Error('Missing parameter!');
}
return bar;
}```

```mandatory = () => {
throw new Error('Missing parameter!');
}

foo = (bar = mandatory()) => {
return bar;
}```

### 17. Array.find 简写

```const pets = [
{ type: 'Dog', name: 'Max'},
{ type: 'Cat', name: 'Karl'},
{ type: 'Dog', name: 'Tommy'},
]

function findDog(name) {
for(let i = 0; i<pets.length; ++i) {
if(pets[i].type === 'Dog' && pets[i].name === name) {
return pets[i];
}
}
}```

```pet = pets.find(pet => pet.type ==='Dog' && pet.name === 'Tommy');
console.log(pet); // { type: 'Dog', name: 'Tommy' }```

### 18. 简写 Object[key]

```function validate(values) {
if(!values.first)
return false;
if(!values.last)
return false;
return true;
}

console.log(validate({first:'Bruce',last:'Wayne'})); // true```

```// 对象验证规则
const schema = {
first: {
required:true
},
last: {
required:true
}
}

// 通用验证函数
const validate = (schema, values) => {
for(field in schema) {
if(schema[field].required) {
if(!values[field]) {
return false;
}
}
}
return true;
}

console.log(validate(schema, {first:'Bruce'})); // false
console.log(validate(schema, {first:'Bruce',last:'Wayne'})); // true```

### 19. 简写双重按位非运算符

`Math.floor(4.9) === 4  //true`

`~~4.9 === 4  //true`

111 篇文章43 人订阅

0 条评论

## 相关文章

1.5K7

3882

### Luminous版本PG 分布调优

Luminous版本开始新增的balancer模块在PG分布优化方面效果非常明显，操作也非常简便，强烈推荐各位在集群上线之前进行这一操作，能够极大的提升整个集群...

3715

89111

### Mix 10 上的asp.net mvc 2的相关Session

Beyond File | New Company: From Cheesy Sample to Social Platform Scott Hansel...

2787

### sqlserver使用存储过程跟踪SQL

USE [master] GO /****** Object: StoredProcedure [dbo].[sp_perfworkload_trace_s...

3040

### LINQ via C# 系列文章

LINQ via C# Recently I am giving a series of talk on LINQ. the name “LINQ via C...

3025

### js登录滑动验证，不滑动无法登陆

js的判断这里是根据滑块的位置进行判断，应该是用一个flag判断 <%@ page language="java" contentType="text/html...

8908