六个漂亮的 ES6 技巧

经过参数默认值强制要求传参数组

 

ES6 指定默认参数在它们被实际使用的时候才会被执行,这个特性让咱们能够强制要求传参:ide

 

/**函数

* Called if a parameter is missing and测试

* the default value is evaluated.优化

*/ui

function mandatory() {编码

    throw new Error("Missing parameter");lua

}spa

function foo(mustBeProvided = mandatory()) {code

    return mustBeProvided;

}

 

函数调用 mandatory() 只有在参数 mustBeProvided 缺失的时候才会被执行。

 

在控制台测试:

 

> foo()

Error: Missing parameter

> foo(123)

123

 

更多内容:

 

  • 段落: “Required parameters” 。

 

经过 for-of 循环来遍历数组元素和索引

 

方法 forEach() 容许你遍历一个数组的元素和索引:

 

var arr = ["a", "b", "c"];

arr.forEach(function (elem, index) {

    console.log("index = "+index+", elem = "+elem);

});

// Output:

// index = 0, elem = a

// index = 1, elem = b

// index = 2, elem = c

 

ES6 的 for-of 循环支持 ES6 迭代(经过 iterables 和 iterators)和解构。若是你经过数组的新方法 enteries() 再结合解构,能够达到上面 forEach 一样的效果:

 

const arr = ["a", "b", "c"];

for (const [index, elem] of arr.entries()) {

    console.log(`index = ${index}, elem = ${elem}`);

}

 

arr.enteries() 经过索引-元素配对返回一个可迭代对象。而后经过解构数组 [index, elem] 直接获得每一对元素和索引。console.log() 的参数是 ES6 中的模板字面量特性,这个特性带给字符串解析模板变量的能力。

 

更多内容:

 

  • 章节: “Destructuring”

  • 章节: “Iterables and iterators”

  • 段落: “Iterating with a destructuring pattern”

  • 章节: “Template literals”

 

遍历 Unicode 表示的字符串

 

一些 Unicode 编码的字由两个 JavaScript 字符组成,例如,emoji 表情:

 

 

字符串实现了 ES6 迭代,若是你经过迭代来访问字符串,你能够得到编码过的单个字(每一个字用 1 或 2 个 JavaScript 字符表示)。例如:

 

for (const ch of "xuD83DuDE80y") {

    console.log(ch.length);

}

// Output:

// 1

// 2

// 1

 

这让你可以很方便地获得一个字符串中实际的字数:

 

> [..."xuD83DuDE80y"].length

3

 

展开操做符 (...) 将它的操做对象展开并插入数组。

 

更多内容:

 

  • 章节: “Unicode in ES6”

  • 段落: “The spread operator (...)”

 

经过变量解构交换两个变量的值

 

若是你将一对变量放入一个数组,而后将数组解构赋值相同的变量(顺序不一样),你就能够不依赖中间变量交换两个变量的值:

 

[a, b] = [b, a];

 

能够想象,JavaScript 引擎在将来将会针对这个模式进行特别优化,去掉构造数组的开销。

 

更多内容:

 

  • 章节: “Destructuring”

 

经过模板字面量(template literals)进行简单的模板解析

 

ES6 的模板字面量与文字模板相比,更接近于字符串字面量。可是,若是你将它们经过函数返回,你可使用他们来作简单的模板渲染:

 

const tmpl = addrs => `

    

    ${addrs.map(addr => `

        ${addr.first}${addr.last}

    `).join("")}

    

`;

 

tmpl 函数将数组 addrs 用 map(经过箭头函数) join 拼成字符串。tmpl() 能够批量插入数据到表格中:

 

const data = [

    { first: "", last: "Bond" },

    { first: "Lars", last: "" },

];

console.log(tmpl(data));

// Output:

// //

//    

//     Bond

//

//     Lars

//    

//

//

 

更多内容:

 

  • 博客文章: “Handling whitespace in ES6 template literals”

  • 段落: “Text templating via untagged template literals”

  • 章节: “Arrow functions”

 

经过子类工厂实现简单的合成器

 

当 ES6 类继承另外一个类,被继承的类能够是经过任意表达式建立的动态类:

 

// Function id() simply returns its parameter

const id = x => x;

 

class Foo extends id(Object) {}

 

这个特性能够容许你实现一种合成器模式,用一个函数来将一个类 C 映射到一个新的继承了C的类。例如,下面的两个函数 Storage 和 Validation 是合成器:

 

const Storage = Sup => class extends Sup {

    save(database) { ··· }

};

const Validation = Sup => class extends Sup {

    validate(schema) { ··· }

};

 

你可使用它们去组合生成一个以下的 Employee 类:

 

class Person { ··· }

class Employee extends Storage(Validation(Person)) { ··· }

 

更多信息:

 

  • 段落: “Simple mixins”

下面的两个章节提供了很好地归纳了 ECMAScript 6 的特性:

  • An overview of what’s new in ES6

  • First steps with ECMAScript 6 [features that are easy to adopt]

相关文章
相关标签/搜索