文章转自 http://mp.weixin.qq.com/s/4FFjRB-YcerAnYP_qTDMfQ
普通写法:
let answer;
if (x > 10) {
answer = 'is greater';
} else {
answer = 'is lesser';
}
普通写法:
let variable2 = variable1;
}
let variable2 = variable1 || '';
console.log(variable2 === ''); // prints true
variable1 = 'foo';
variable2 = variable1 || '';
console.log(variable2); // prints foo
普通写法:
let y;
let z = 3;
普通写法:
还有一个例子,如果a不等于true,然后do something。
普通写法:
if ( a !== true ) {
// do something...
}
if ( a !== true ) {
// do something...
}
普通写法:
简写:
console.log("a[" + index + "] = " + element);
}
[2, 5, 9].forEach(logArrayElements);
// logs:
// a[0] = 2
// a[1] = 5
// a[2] = 9
普通写法:
if (process.env.DB_HOST) {
dbHost = process.env.DB_HOST;
} else {
dbHost = 'localhost';
}
普通写法:
// All the below will evaluate to true
1e0 === 1;
1e1 === 10;
1e2 === 100;
1e3 === 1000;
1e4 === 10000;
1e5 === 100000;
普通写法:
普通写法:
console.log('Hello', name);
}
setTimeout(function() {
console.log('Loaded')
}, 2000);
list.forEach(function(item) {
console.log(item);
});
setTimeout(() => console.log('Loaded'), 2000);
list.forEach(item => console.log(item));
为了返回一个多行的声明,使用()而不是{}包裹你的方法体,这将确保代码在一个单独的声明里被求值。
普通写法:
return Math.PI * diameter
}
Math.PI * diameter;
)
普通写法:
if (w === undefined)
w = 3;
if (h === undefined)
h = 4;
return l * w * h;
}
volume(2) //output: 24
普通写法:
const db = 'http://' + host + ':' + port + '/' + database;
const db = `http://${host}:${port}/${database}`;
普通写法:
const action = require('mobx/action');
const runInAction = require('mobx/runInAction');
const store = this.props.store;
const form = this.props.form;
const loading = this.props.loading;
const errors = this.props.errors;
const entity = this.props.entity;
const { store, form, loading, errors, entity } = this.props;
普通写法:
+ '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'
简写:
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.`
普通写法:
const odd = [1, 3, 5];
const nums = [2 ,4 , 6].concat(odd);
// cloning arrays
const arr = [1, 2, 3, 4];
const arr2 = arr.slice()
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 nums = [2, ...odd, 4 , 6];
console.log(a) // 1
console.log(b) // 2
console.log(z) // { c: 3, d: 4 }
普通语法:
if(bar === undefined) {
throw new Error('Missing parameter!');
}
return bar;
}
throw new Error('Missing parameter!');
}
foo = (bar = mandatory()) => {
return bar;
}
普通写法:
{ 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];
}
}
}
console.log(pet); // { type: 'Dog', name: 'Tommy' }
考虑下这个简单的validate方法的例子:
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
}
}
// universal validation function
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
然而,一个非常实用的用例双位操作符。你可以用它代替Math.floor()。双位操作符的优势是,它执行相同的操作要快得多。
普通写法: