Node.js v6里有哪些新东西?
发布于 8 年前 作者 i5ting 4875 次浏览 来自 分享

Node.js v6里有哪些新东西?

原文

性能提升

安全提升

新的ES6特性

Default function parameters

function multiply(a, b = 1) {  
  return a * b
}

multiply(5) // 5  

Learn more on the default function parameters.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/default_parameters

Rest parameters

function fun1(...theArgs) {  
  console.log(theArgs.length)
}

fun1()  // 0  
fun1(5) // 1  
fun1(5, 6, 7) // 3  

Learn more on the rest parameters.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/rest_parameters

Spread operator

// before the spread operator
function myFunction(x, y, z) { }  
var args = [0, 1, 2]  
myFunction.apply(null, args)

// with the spread operator
function myFunction(x, y, z) { }  
var args = [0, 1, 2]  
myFunction(...args)

Learn more on the spread operator.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator

Destructuring

var x = [1, 2, 3, 4, 5]  
var [y, z] = x  
console.log(y) // 1  
console.log(z) // 2  

Learn more on destructuring.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

new.target

function Foo() {  
  if (!new.target) throw 'Foo() must be called with new'
  console.log('Foo instantiated with new')
}

Foo() // throws "Foo() must be called with new"  
new Foo() // logs "Foo instantiated with new"  

Learn more on the new.target.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new.target

Proxy

The Proxy object is used to define custom behavior for fundamental operations.

var handler = {  
    get: function(target, name){
        return name in target ? target[name] : 37
    }
};

var p = new Proxy({}, handler)  
p.a = 1  
p.b = undefined

console.log(p.a, p.b) // 1, undefined  
console.log('c' in p, p.c) // false, 37  

Learn more on proxies.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Proxy

Reflect

Reflect is a built-in object that provides methods for interceptable JavaScript operations.

Learn more on reflect.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect

Symbols

A symbol is a unique and immutable data type and may be used as an identifier for object properties.

Symbol("foo") === Symbol("foo"); // false  

Learn more on symbols.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Symbol

尝试一下热乎的 Node.js v6吧

If you are using nvm, you only have to:

nvm install 6  

If you for some reason cannot you nvm, you can download the binary from the official Node.js website.

https://nodejs.org/en

什么时候可以迁移到Node 6呢?

今年10月份 Node.js v6 会升级到LTS 版本 - 那时你就可以正式的产品环境使用了,目前还不能确定完全稳定。

全文完

欢迎关注我的公众号【node全栈】

node全栈.png

如果想参与评论,请点击原文链接,进入国内最专业的cnode论坛

3 回复

感觉越来越爽了

不错!刚想去看看多了些啥 。

回到顶部