fat32格式化函数_基本的ES6 Fat Arrow => Java中的函数语句

fat32格式化函数

ES6 or ECMAScript 2015 introduced a new way of writing Javascript functions. This used the arrow function statement, also called the “fat arrow” denoted by the “ => “ notation. It is supported by various web browsers that are based on the Chromium engine (e.g. Google Chrome, Firefox), so it has wide reach among developers. It saves time and simplifies writing functions. What it provides is a short cut for developers, but requires learning a new way to write the code.

ES6ECMAScript 2015引入了一种编写Javascript函数的新方法。 这使用了箭头函数语句 ,也称为“胖箭头”,用“ =>”符号表示。 它受基于Chromium引擎的各种Web浏览器(例如Google Chrome,Firefox)支持,因此在开发人员中具有广泛的影响。 它可以节省时间并简化编写功能。 它为开发人员提供了捷径,但需要学习一种新的代码编写方式。

功能说明 (The Function Statement)

The basic function statement is an expression that is a block of code that execute routines and methods contained within its scope. A function must be called or invoked in order to execute. It is written by declaring the function and its routines and methods contained between { }. Here is a simple example in traditional Javascript notation:

基本函数语句是一个表达式,它是一段代码,可以执行其范围内包含的例程和方法。 必须调用或调用一个函数才能执行。 它是通过声明{}之间的函数及其例程和方法来编写的。 这是传统Javascript表示法中的一个简单示例:

var addTwoNumbers = function(x, y) {
return x + y;
};

In its simplest form you are taking the sum of two numbers (integers by default) with a value of x and y and returning its result. This is how developers have been coding prior to ES6.

以最简单的形式,您将取两个具有x和y值的数字(默认为整数)之和,并返回其结果。 这就是开发人员在ES6之前进行编码的方式。

箭头功能声明 (The Arrow Function Statement)

When using ES6 notation syntax, fewer lines of code are required and appears much more simplified:

使用ES6表示法语法时,所需的代码行更少,并且看起来更加简化:

var addTwoNumbersArrow = (x, y) => { return x + y };

The ‘function’ keyword in the statement is removed and replaced by the “ ( ) “ and the “fat arrow” or “ => “. The statement fits in one line. Developers who need to code large applications can save time coding by issuing arrow functions rather than having to write out the entire statement with the ‘function’ keyword all the time.

语句中的“功能”关键字已删除,并由“()”和“胖箭头”或“ =>”代替。 该语句适合一行。 需要编写大型应用程序的开发人员可以通过发布箭头函数来节省时间编码,而不必始终用'function'关键字写出整个语句。

声明函数 (Declaring Functions)

There are two types of function declarations we can do. We can use a parameter as an argument or use no parameter in a function statement.

我们可以执行两种类型的函数声明。 我们可以将参数用作参数,也可以在函数语句中不使用参数。

This is your example of a typical function statement with a simple parameter:

这是带有简单参数的典型函数语句的示例:

var hello = function(val) {
var val = "Everybody"
return "Hello " + val;
};
Hello Everybody

This can be written in ES6 like so:

可以这样在ES6中编写:

hello = (val) => "Hello " + val;

Using the console, the ES6 code can be implemented like in this example:

使用控制台,可以像以下示例一样实现ES6代码:

> hello = (val) => "Hello " + val;
> var demo = hello("Man");
> demo
Hello Man

To see how arrow functions have simplified writing functions consider the next example. Here is a typical function statement with no parameter:

要查看箭头功能如何简化编写功能,请考虑下一个示例。 这是不带参数的典型函数语句:

var greet = function() {
return "Hello World!";
};
Hello World

With an arrow function, all the developer needs to do is remove the function keyword like so:

使用箭头功能,开发人员所需要做的就是删除function关键字,如下所示:

var greet = () => {
return "Hello World!";
};
Hello World

The above code can be even further simplified in ES6. If the function has just one statement that returns a value, the brackets can be removed along with the “return” keyword. You don’t even need to include the “var” keyword but that would not be good practice when it comes to more complex programs. It can be written like in this example:

上面的代码甚至可以在ES6中进一步简化。 如果函数只有一个返回值的语句 ,则可以将括号和“ return”关键字一起删除。 您甚至不需要包含“ var”关键字,但是对于更复杂的程序,这并不是一个好习惯。 可以这样写:

greet = () => "Hello World!";Hello World

带有阵列的胖箭头 (Fat Arrow With Arrays)

Here is a use case example of how to code more neatly using the ES6 arrow function. Let us say you are building a list of products using an array for an online retailer. First you build the array of elements with a name of the product and its unit price:

这是一个用例示例,说明如何使用ES6箭头功能更简洁地编码。 假设您正在使用用于在线零售商的阵列构建产品列表。 首先,使用产品名称及其单价构建元素数组:

const products = [
{ name:'iPad Pro', unitPrice:799 },
{ name:'iPhone 11 Pro Max', unitPrice:1099 },
{ name:'AirPods', unitPrice:159 }
];

With ES5 and lower, to list the array by product name we would code it like so:

对于ES5及更低版本,要按产品名称列出阵列,我们将这样编码:

var productName = products.map(function(products) {
return products.name;
});
console.log(productName)
(3) ["iPad Pro", "iPhone 11 Pro Max", "AirPods"]

In ES6 it can be simplified into one line of code, making it more concise with less clutter:

在ES6中,它可以简化为一行代码,从而使其更简洁,更简洁:

var productName = products.map(products => products.name);console.log(productName)
(3) ["iPad Pro", "iPhone 11 Pro Max", "AirPods"]

Likewise you can list the product items by their unit prices:

同样,您可以按产品单价列出它们:

var productPrices = products.map(products => products.unitPrice);console.log(productPrices)
(3) [799, 1099, 159]

摘要 (Summary)

Arrow function statements help simplify complex coding, reducing development time when building applications. They can be used in place of traditional syntax. It also makes the code easier to read, which looks organized and has less clutter.

箭头函数语句有助于简化复杂的编码,从而减少了构建应用程序时的开发时间。 可以使用它们代替传统语法。 这也使代码更易于阅读,看起来井井有条,杂乱无章。

翻译自: https://medium.com/0xcode/basic-es6-fat-arrow-function-statements-in-javascript-325646ae57aa

fat32格式化函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值