现在升级到Node V4的七个理由

原地址:http://www.cli-nerd.com/2015/09/09/7-reasons-to-upgrade-to-node-v4-now.html

今天Node v4发布了。这是第一个在io.js合并后的
第一个稳定版本,给我们带来了一堆亮点,如新的ES6语法的添加。虽然有已经增加了很好的ES6的描述,但是这篇文章展示了如何使用它们!

特别是,我要去以下ES6补充的地址看看:

Template Strings
Classes
Arrow Functions
Object Literals
Promises
String Methods
let and const

1. Template Strings(模板字符串)

如果你要用Javascript创建多行字符串,你或许会像下面的做法:

1
2
3
4
5
var message = [
'The quick brown fox',
'jumps over',
'the lazy dog'
].join('\n');

虽然这适用于少量的文字,但会多个语句变得混乱。因此,聪明的开发者想出了一个叫hack called multiline的模块:

1
2
3
4
5
6
var multiline = require('multiline');
var message = multiline(function () {/*
The quick brown fox
jumps over
the lazy dog
*/});

幸运的是,ES6给我们带来了模板字符串:

1
2
3
4
5
var message = `
The quick brown fox
jumps over
the lazy dog
`;

在这个特性里,还可以进行字符串的赋值操作

1
2
3
4
5
6
7
8
9
var name = 'Schroedinger';
// stop doing this ...
var message = 'Hello ' + name + ', how is your cat?';
var message = ['Hello ', name, ', how is your cat?'].join('');
var message = require('util').format('Hello %s, how is your cat?', name);
// and instead do that ...
var message = `Hello ${name}, how is your cat?`;

查看更多template strings的详情在MDN.

2. Classes(类)

定义类ES5看起来有些奇怪,还需要一定的时间去习惯:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
var Pet = function (name) {
this._name = name;
};
Pet.prototype.sayHello = function () {
console.log('*scratch*');
};
Object.defineProperty(Pet.prototype, 'name', {
get: function () {
return this._name;
}
});
var Cat = function (name) {
Pet.call(this, name);
};
require('util').inherits(Cat, Pet);
Cat.prototype.sayHello = function () {
Pet.prototype.sayHello.call(this);
console.log('miaaaauw');
};

幸运的是,在Node中我们可以使用新的ES6语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Pet {
constructor(name) {
this._name = name;
}
sayHello() {
console.log('*scratch*');
}
get name() {
return this._name;
}
}
class Cat extends Pet {
constructor(name) {
super(name);
}
sayHello() {
super.sayHello();
console.log('miaaaauw');
}
}

一个extends关键字,constructors,调用父类和属性。多好?更多在MDN有完整的指南

3. Arrow Functions(箭头函数)

动态的绑定函数调用的结合总是会引起一些混乱,人们在多个方面围绕它的工作:

1
2
3
4
5
6
7
8
9
10
11
Cat.prototype.notifyListeners = function () {
var self = this;
this._listeners.forEach(function (listener) {
self.notifyListener(listener);
});
};
Cat.prototype.notifyListeners = function () {
this._listeners.forEach(function (listener) {
this.notifyListener(listener);
}.bind(this));
};

现在只需要使用箭头函数:

1
2
3
4
5
Cat.prototype.notifyListeners = function () {
this._listeners.forEach((listener) => {
this.notifyListener(listener);
});
};

查看arrow functions更多详情.

4. Object Literals(对象直接量)

当使用对象直接量,你可以使用很不错的快捷方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var age = 10, name = 'Petsy', size = 32;
// instead of this ...
var cat = {
age: age,
name: name,
size: size
};
// ... do this ...
var cat = {
age,
name,
size
};

此外,您现在可以轻松的将函数添加到您的对象直接量上

5. Promises

可以替换掉用第三方的像bluebird和Q的依赖,直接使用原生的promises。就像如下的api:

1
2
3
4
5
6
7
8
var p1 = new Promise(function (resolve, reject) {});
var p2 = Promise.resolve(20);
var p3 = Promise.reject(new Error());
var p4 = Promise.all(p1, p2);
var p5 = Promise.race(p1, p2);
// and obviously
p1.then(() => {}).catch(() => {});

6. String Methods

我们还可以使用一些新的字符串工具方法:

1
2
3
4
5
6
7
// replace `indexOf()` in a number of cases
name.startsWith('a')
name.endsWith('c');
name.includes('b');
// repeat the string three times
name.repeat(3);

去告诉那些写ruby的孩子!字符串现在还更好的支持unicode的处理。

7. let and const

猜下这个在如下函数调用后返回的值:

1
2
3
4
5
6
7
var x = 20;
(function () {
if (x === 20) {
var x = 30;
}
return x;
}()); // -> undefined

是的,undefined。替换掉var,使用let你可以保证预期的结果:

1
2
3
4
5
6
7
let x = 20;
(function () {
if (x === 20) {
let x = 30;
}
return x;
}()); // -> 20

理由是:var是函数作用域级的,然而let是块作用域级别的(这是人们所希望)。因为这个,说let是安全的var。你可以在MDN上获取更多关于let的信息

新增:Node现在支持const关键字,从而阻止您重新赋给一个不同的值相同的引用:

1
2
3
4
var MY_CONST = 42; // no, no
const MY_CONST = 42; // yes, yes
MY_CONST = 10 // with const, this is no longer possible

综述所述

Node v4带来了更多的ES6的特性,同时我也希望这些7个例子已经使你信服升级和使用最新版本了。

这些更多的语言特性(像 maps/sets, symbols , generators,等)。请确保你已经查看过ES6补充的NodeV4概述。快乐升级!