let 声明
- for 循环,使用 let 声明变量在块级作用域内
for (let i = 0; i < a.length; i++) { let x = a[i] … } for (let i = 0; i < b.length; i++) { let y = b[i] … } let callbacks = [] for (let i = 0; i <= 2; i++) { callbacks[i] = function () { return i * 2 } } callbacks[0]() === 0 callbacks[1]() === 2 callbacks[2]() === 4 ECMAScript 5 var i, x, y; for (i = 0; i < a.length; i++) { x = a[i]; … } for (i = 0; i < b.length; i++) { y = b[i]; … } var callbacks = []; for (var i = 0; i <= 2; i++) { (function (i) { callbacks[i] = function() { return i * 2; }; })(i); } callbacks[0]() === 0; callbacks[1]() === 2; callbacks[2]() === 4;
箭头函数
- 箭头函数 =>
odds = evens.map(v => v + 1) pairs = evens.map(v => ({ even: v, odd: v + 1 })) nums = evens.map((v, i) => v + i) ECMAScript 5 odds = evens.map(function (v) { return v + 1; }); pairs = evens.map(function (v) { return { even: v, odd: v + 1 }; }); nums = evens.map(function (v, i) { return v + i; });
扩展运算符
- 扩展运算符替换 concat
var params = [ "hello", true, 7 ] var other = [ 1, 2, ...params ] // [ 1, 2, "hello", true, 7 ] var params = [ "hello", true, 7 ]; var other = [ 1, 2 ].concat(params); // [ 1, 2, "hello", true, 7 ]
- 扩展运算符 … 为 function 赋值
function f (x, y, ...a) { return (x + y) * a.length } f(1, 2, ...params) === 9 function f (x, y) { var a = Array.prototype.slice.call(arguments, 2); return (x + y) * a.length; }; f.apply(undefined, [ 1, 2 ].concat(params)) === 9;
- 扩展运算符 … 代替 split
var str = "foo"; var chars = str.split(""); // [ "f", "o", "o" ] const chars = [ ...str ] // [ "f", "o", "o" ] const chars = Array.from(str); // ["f", "o", "o"]
- 找出最大、最小值
const arr = [2, 8, 15, 4]; Math.max(...arr); // 15 Math.min(...arr); // 2
解构赋值
- 为函数 function 的参数指定默认值,省去了值为空的判断
function f (x, y = 7, z = 42) { return x + y + z } f(1) === 50 function f (x, y, z) { if (y === undefined) y = 7; if (z === undefined) z = 42; return x + y + z; }; f(1) === 50;
- 声明变量
let x, y = 20;
let x;
let y = 20;
- 多个变量赋值
let [a, b, c] = [5, 8, 12];
let a, b, c;
a = 5;
b = 8;
c = 12;
- 对象简洁赋值
obj = { x, y } obj = { x: x, y: y };
模板字符串
- 变量和文字的混排使用单斜括号` ` 和带 $ 的大括号 {}
var customer = { name: "Foo" } var card = { amount: 7, product: "Bar", unitprice: 42 } var message = `Hello ${customer.name}, want to buy ${card.amount} ${card.product} for a total of ${card.amount * card.unitprice} bucks?` var message = "Hello " + customer.name + ",\n" + "want to buy " + card.amount + " " + card.product + " for\n" + "a total of " + (card.amount * card.unitprice) + " bucks?";
- url 路径的书写
get(`http://example.com/foo?bar=${bar + baz}&quux=${quux}`) get([ "http://example.com/foo?bar=", "&quux=", "" ],bar + baz, quux);
- 动态属性名
let obj = { foo: "bar", [ "baz" + quux() ]: 42 } var obj = { foo: "bar" }; obj[ "baz" + quux() ] = 42;
Object
- 比较两个值是否相等
Object.is('foo', 'foo') // true
Object.is({}, {}) // false
Object.is(+0, -0) // false
Object.is(NaN, NaN) // true
对象的合并
Object.assign(target, source1, source2); //将源对象(source)的所有可枚举属性,复制到目标对象(target) //同名属性,则后面的属性会覆盖前面的属性 //参数不是对象,则会先转成对象 //如果源对象某个属性的值是对象,那么目标对象拷贝得到的是这个对象的引用 //可以用来处理数组,但是会把数组视为对象 //为对象添加属性 Object.assign(this, {x, y}); //为属性指定默认值
options = Object.assign({}, DEFAULTS, options);
- 克隆对象
function clone(origin) { return Object.assign({}, origin); }
数组
- includes 某个数组是否包含给定的值
if ([1, 'one', 2, 'two'].indexOf(value) !== 1) { // 原来的方法,不能判断NAN } if ([1, 'one', 2, 'two'].includes(value)) { // ES6 方法 } if (value === 1 || value === 'one' || value === 2 || value === 'two') { // ... }
- 数组去重
[...new Set(array)]
Class
- class
class baseModel { constructor(options = {}, data = []) { // class constructor this.name = 'Base' this.url = 'http://azat.co/api' this.data = data this.options = options } getName() { // class method console.log(`Class name: ${this.name}`) } } //继承 baseModel class AccountModel extends baseModel { constructor(options, data) { super({private: true}, ['32113123123', '524214691']) //super 来继承父级的方法 this.name = 'Account Model' this.url +='/accounts/' } } let accounts = new AccountModel(5) accounts.getName() console.log('Data is %s', accounts.accountsData) 输出 => Class name: Account Model Data is %s 32113123123,524214691
modules
- modules
import export
export var port = 3000
export function getAccounts(url) {
...
}
import {port, getAccounts} from 'module' console.log(port) // 3000
import * as service from 'module' console.log(service.port) // 3000
参考资料: