by rajaraodv

通过rajaraodv

以下是ECMAScript 2016、2017和2018中所有新增功能的示例 (Here are examples of everything new in ECMAScript 2016, 2017, and 2018)

It’s hard to keep track of what’s new in JavaScript (ECMAScript). And it’s even harder to find useful code examples.

很难跟踪JavaScript(ECMAScript)的新功能。 而且,找到有用的代码示例更加困难。

So in this article, I’ll cover all 18 features that are listed in the TC39’s finished proposals that were added in ES2016, ES2017, and ES2018 (final draft) and show them with useful examples.

因此,在本文中,我将介绍TC39最终提案中列出的全部18个功能,这些提案已添加到ES2016,ES2017和ES2018(最终草案)中,并通过有用的示例进行展示。

This is a pretty long post but should be an easy read. Think of this as “Netflix binge reading.” By the end of this, I promise that you’ll have a ton of knowledge about all these features.

这是一篇相当长的文章,但应该很容易阅读。 将此视为“ Netflix暴饮暴食”。 到此为止,我保证您将对所有这些功能有很多了解。

好,让我们一一介绍。 (OK, let’s go over these one by one.)

1. Array.prototype.includes (1. Array.prototype.includes)

includes is a simple instance method on the Array and helps to easily find if an item is in the Array (including NaN unlike indexOf).

includes是Array上的一个简单实例方法,可帮助轻松查找是否在Array中(包括与indexOf不同的NaN )。

Trivia: the JavaScript spec people wanted to name it contains , but this was apparently already used by Mootools so they used includes .

Trivia:人们希望将JavaScript规范命名为contains ,但Mootools显然已经使用了它,因此他们使用了includes

2.求幂中infix operator (2. Exponentiation infix operator)

Math operations like addition and subtraction have infix operators like + and - , respectively. Similar to them, the ** infix operator is commonly used for exponent operation. In ECMAScript 2016, the ** was introduced instead of Math.pow .

数学运算(例如加法和减法)分别具有中缀运算符(例如+- )。 与它们相似, ** infix运算符通常用于指数运算。 在ECMAScript 2016中,引入了**而不是Math.pow

1. Object.values() (1. Object.values())

Object.values() is a new function that’s similar to Object.keys() but returns all the values of the Object’s own properties excluding any value(s) in the prototypical chain.

Object.values()是一个类似于Object.keys()的新函数,但返回对象自身属性的所有值,但不包括原型链中的任何值。

2. Object.entries() (2. Object.entries())

Object.entries() is related to Object.keys , but instead of returning just keys, it returns both keys and values in the array fashion. This makes it very simple to do things like using objects in loops or converting objects into Maps.

Object.entries()Object.keys有关,但它不仅返回键,还以数组形式返回键和值。 这样就很容易执行诸如在循环中使用对象或将对象转换为Map之类的事情。

Example 1:

范例1:

Example 2:

范例2:

3.字符串填充 (3. String padding)

Two instance methods were added to String — String.prototype.padStart and String.prototype.padEnd — that allow appending/prepending either an empty string or some other string to the start or the end of the original string.

在String中添加了两个实例方法String.prototype.padStartString.prototype.padEnd允许将空字符串或其他字符串附加/添加到原始字符串的开头或结尾。

'someString'.padStart(numberOfCharcters [,stringForPadding]); '5'.padStart(10) // '         5'
'5'.padStart(10, '=*') //'=*=*=*=*=5''5'.padEnd(10) // '5         '
'5'.padEnd(10, '=*') //'5=*=*=*=*='

This comes in handy when we want to align things in scenarios like pretty print display or terminal print.

当我们想要在漂亮的打印显示器或终端打印等场景中对齐内容时,这非常方便。

3.1 padStart示例: (3.1 padStart example:)

In the below example, we have a list of numbers of varying lengths. We want to prepend “0” so that all the items have the same length of 10 digits for display purposes. We can use padStart(10, '0') to easily achieve this.

在下面的示例中,我们列出了不同长度的数字。 我们想在前面加上“ 0”,以便所有项目都具有相同的10位数字长度以用于显示。 我们可以使用padStart(10, '0')轻松实现这一目标。

3.2 padEnd示例: (3.2 padEnd example:)

padEnd really comes in handy when we are printing multiple items of varying lengths and want to right-align them properly.

当我们要打印长度不同的多个项目并希望正确对齐它们时, padEnd确实派上了用场。

The example below is a good realistic example of how padEnd , padStart , and Object.entries all come together to produce a beautiful output.

下面的示例是一个很好的现实示例,说明padEndpadStartObject.entries如何共同产生漂亮的输出。

const cars = {'												

以下是ECMAScript 2016、2017和2018中所有新增功能的示例相关推荐

  1. C#6中的新增功能 【Unity3D亲测】

    随着Unity2017的普及,使用.NET 4X的开发者也越来越多了,下面笔者给大家介绍一下在C# 6中的新功能主要是怕自己忘记,2333,有些功能还是很实用的~ 使用Unity版本2018.2.9f ...

  2. C# 9.0中的新增功能 - 译

    https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9 What's new in C# 9.0 C# 9.0中的新增功能 ...

  3. KSO-.NET 6 中的新增功能

    .NET 6 中的新增功能 .NET 6 提供 .NET 统一计划的最终部分,该计划在 .NET 5 中启动. .NET 6 在移动.桌面.IoT 和云应用之间统一了 SDK.基础库和运行时. 除了这 ...

  4. .Net 6.0中的新增特性_.Net 6.0中的新增功能

    .Net 6.0中的新增特性_.Net 6.0中的新增功能 一..Net 6 介绍 .NET 6 作为 LTS 长期支持版本,.NET 6 将会获得 3 年的技术支持. .NET 6 是首个原生支持 ...

  5. 王牌代码静态测试工具Helix QAC 2022.2 中的新增功能(1)

    Helix QAC 2022.2 中的新增功能 Helix QAC 2022.2 提供更强大的 C++ 20语言功能支持,更新了 MISRA 和 HKMC 的合规性模块,以及新的配置选项,可在处理 S ...

  6. 速览Visual Studio 2022 中的新增功能

    目录 性能改进 Visual Studio 2022 为 64 位 在文件中更快地查找 Git 工具速度更快 生成新式应用 适用于 C++..NET 和热重载的更佳开发工具 Blazor & ...

  7. sql server新增列_SQL Server 2017中的新增功能

    sql server新增列 SQL Server 2017 is considered a major release in the history of the SQL Server life cy ...

  8. sql server新增列_SQL Server 2016安装中的新增功能?

    sql server新增列 SQL Server 2016 introduced officially on the 1st of June 2016. It comes with many new ...

  9. 服务器创建和附加虚拟磁盘,Windows 7 虚拟硬盘中的新增功能

    [IT168 专稿]Microsoft 虚拟硬盘文件格式 (.vhd) 是一种公用格式规范,用于指定封装在单个文件中的虚拟硬盘,它能够承载本机文件系统并支持标准的磁盘操作. Microsoft Win ...

最新文章

  1. android检测本地是否安装,在本地测试模块的安装
  2. 完美解释了递归,哈哈哈哈
  3. Asp.Net 学习资源列表
  4. Xamarin Essentials教程振动Vibration
  5. JavaScript - Closure
  6. 【示例】Lucene创建索引库编程步骤
  7. linux python连接oracle数据库_Linux下通过python访问MySQL、Oracle、SQL Server数据库的方法...
  8. Chapter 1. Asp.Net 概述
  9. 必备知识:工业相机相关知识(初学者必备)
  10. 书摘—20几岁不学应酬,30几岁只能发愁
  11. 安装安全防护软件有助于保护计算机不受侵害,安装安全防护软件有助于保护计算机不受病毒侵害。...
  12. 基于PC的工业控制系统的优缺点分析
  13. 电子书沦为“压泡面”神器,其实高端电子书就该从这两个里边选
  14. 鸿蒙电视应用beta,华为鸿蒙 2.0 开发者公测版 Beta 3 更新:增加三个鸿蒙应用
  15. 微信支付和支付宝支付整合(含设计模式1)
  16. Python+OpenCV 调用手机摄像头并实现人脸识别
  17. jupyter notebook 代码自动补全、代码高亮
  18. 个人项目 小跟班——蓝牙控制小车(蓝牙控制篇)
  19. 51nod 1421 最大MOD值(高妙的调和级数复杂度)
  20. 关于海外问卷调查的一些问题

热门文章

  1. 你必须知道的CSS盒模型,顺利通过阿里面试
  2. 牛笔了!Android面试真题解析火爆全网,面试必备
  3. 处理效应模型stata实例_stata︱政策处理效应模型sata基本命令汇总
  4. C++类的数组元素查找最大值问题
  5. HttpClient 学习整理(转)
  6. easyui 布局之window和panel一起使用时,拉动window宽高时panel不跟随一起变化
  7. linux 查看用户上次修改密码的日期
  8. grep与egrep的区别
  9. Zookeeper概述、特点、数据模型
  10. Android软件安全与逆向分析之Dalvik