简体   繁体   English

如何使用箭头功能

[英]How to use a Arrow Function

I'm just reading up on ES6 features that have been implemented in Node v4.0.0 and saw Arrows. 我刚刚阅读了在Node v4.0.0中实现的ES6功能并看到了Arrows。 The example from Arrow Functions is: Arrow Functions的例子是:

var a = [
    "Hydrogen",
    "Helium",
    "Lithium",
    "Beryl­lium"
];
var a2 = a.map(function(s){ return s.length });
var a3 = a.map( s => s.length );

My question is how would I then use multiply lined code inside of a a.map( s => s.length ); 我的问题是如何在a.map( s => s.length );使用多重排列的代码a.map( s => s.length ); Sometimes I need to play with things rather than just returning the length in this example. 有时我需要玩一些东西而不是仅仅返回这个例子中的长度。

I'm assuming its only used for returning the value and nothing else? 我假设它只用于返回值而没有别的?

Just wrap your multiple code lines in curly braces like this: 只需将多个代码行包装成花括号,如下所示:

var a3 = a.map( s => {
    var temp = s.length;
    return temp;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM