阿里云最近在作活动,低至2折,有兴趣能够看看:
https://promotion.aliyun.com/...
为了保证的可读性,本文采用意译而非直译。html
ES10 还只是一个草案。可是除了 Object.fromEntries
以外,Chrome 的大多数功能都已经实现了,为何不早点开始探索呢?当全部浏览器都开始支持它时,你将走在前面,这只是时间问题。前端
在新的语言特性方面,ES10 不如 ES6 重要,但它确实添加了一些有趣的特性(其中一些功能目前还没法在浏览器中工做: 2019/02/21)node
在 ES6 中,箭头函数无疑是最受欢迎的新特性,在 ES10 中会是什么呢?git
BigInt 是第七种 原始类型。github
BigInt 是一个任意精度的整数。这意味着变量如今能够 表示²⁵³
数字,而不只仅是9007199254740992
。正则表达式
const b = 1n; // 追加 n 以建立 BigInt
在过去,不支持大于 9007199254740992
的整数值。若是超过,该值将锁定为 MAX_SAFE_INTEGER + 1
:算法
const limit = Number.MAX_SAFE_INTEGER; ⇨ 9007199254740991 limit + 1; ⇨ 9007199254740992 limit + 2; ⇨ 9007199254740992 <--- MAX_SAFE_INTEGER + 1 exceeded const larger = 9007199254740991n; ⇨ 9007199254740991n const integer = BigInt(9007199254740991); // initialize with number ⇨ 9007199254740991n const same = BigInt("9007199254740991"); // initialize with "string" ⇨ 9007199254740991n
typeof 10; ⇨ 'number' typeof 10n; ⇨ 'bigint'
10n === BigInt(10); ⇨ true 10n == 10; ⇨ true
200n / 10n ⇨ 20n 200n / 20 ⇨ Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions <
-100n ⇨ -100n +100n ⇨ Uncaught TypeError: Cannot convert a BigInt value to a number
当你读到这篇文章的时候,matchAll 可能已经在 Chrome C73 中正式实现了——若是不是,它仍然值得一看。特别是若是你是一个正则表达式(regex)爱好者。数据库
若是您运行谷歌搜索JavaScript string match all,第一个结果将是这样的:如何编写正则表达式“match all”?segmentfault
最佳结果将建议 String.match 与正则表达式和 /g 一块儿使用或者带有 /g 的 RegExp.exec 或者带有 /g 的 RegExp.test 。api
首先,让咱们看看旧规范是如何工做的。
带字符串参数的 String.match 仅返回第一个匹配:
let string = 'Hello'; let matches = string.match('l'); console.log(matches[0]); // "l"
结果是单个 "l"
(注意:匹配存储在 matches[0] 中而不是 matches)
在“hello”
中搜索 "l"
只返回 "l"
。
将 string.match 与 regex 参数一块儿使用也是如此:
让咱们使用正则表达式 /l/
找到字符 串“hello” 中的 “l”
字符:
let string = "Hello"; let matches = string.match(/l/); console.log(matches[0]); // "l"
let string = "Hello"; let ret = string.match(/l/g); // (2) [“l”, “l”];
很好,咱们使用 < ES10 方式获得了多个匹配,它一直起做用。
那么为何要使用全新的 matchAll 方法呢? 在咱们更详细地回答这个问题以前,让咱们先来看看 捕获组。若是不出意外,你可能会学到一些关于正则表达式的新知识。
在 regex 中捕获组只是从 () 括号中提取一个模式,可使用 /regex/.exec(string) 和string.match 捕捉组。
常规捕获组是经过将模式包装在 (pattern) 中建立的,可是要在结果对象上建立 groups
属性,它是: (?<name>pattern)
。
要建立一个新的组名,只需在括号内附加 ?<name>,结果中,分组 (pattern) 匹配将成为 group.name,并附加到 match 对象,如下是一个实例:
字符串标本匹配:
这里建立了 match.groups.color 和 match.groups.bird :
const string = 'black*raven lime*parrot white*seagull'; const regex = /(?<color>.*?)\*(?<bird>[a-z0-9]+)/g; while (match = regex.exec(string)) { let value = match[0]; let index = match.index; let input = match.input; console.log(`${value} at ${index} with '${input}'`); console.log(match.groups.color); console.log(match.groups.bird); }
须要屡次调用 regex.exec 方法来遍历整个搜索结果集。 在每次迭代期间调用.exec 时,将显示下一个结果(它不会当即返回全部匹配项。),所以使用 while 循环。
输出以下:
black*raven at 0 with 'black*raven lime*parrot white*seagull' black raven lime*parrot at 11 with 'black*raven lime*parrot white*seagull' lime parrot white*seagull at 23 with 'black*raven lime*parrot white*seagull' white seagull
但奇怪的是:
若是你从这个正则表达式中删除 /g,你将永远在第一个结果上建立一个无限循环。这在过去是一个巨大的痛苦。想象一下,从某个数据库接收正则表达式时,你不肯定它的末尾是否有 /g,你得先检查一下。
让咱们尝试匹配单词 hello
中字母 e
和 l
的全部实例, 由于返回了迭代器,因此可使用 for…of 循环遍历它:
// Match all occurrences of the letters: "e" or "l" let iterator = "hello".matchAll(/[el]/); for (const match of iterator) console.log(match);
这一次你能够跳过 /g, .matchall
方法不须要它,结果以下:
[ 'e', index: 1, input: 'hello' ] // Iteration 1 [ 'l', index: 2, input: 'hello' ] // Iteration 2 [ 'l', index: 3, input: 'hello' ] // Iteration 3
.matchAll 具备上面列出的全部好处。它是一个迭代器,能够用 for…of 循环遍历它,这就是整个语法的不一样。
const string = 'black*raven lime*parrot white*seagull'; const regex = /(?<color>.*?)\*(?<bird>[a-z0-9]+)/; for (const match of string.matchAll(regex)) { let value = match[0]; let index = match.index; let input = match.input; console.log(`${value} at ${index} with '${input}'`); console.log(match.groups.color); console.log(match.groups.bird); }
请注意已经没有 /g 标志,由于 .matchAll() 已经包含了它,打印以下:
black*raven at 0 with 'black*raven lime*parrot white*seagull' black raven lime*parrot at 11 with 'black*raven lime*parrot white*seagull' lime parrot white*seagull at 23 with 'black*raven lime*parrot white*seagull' white seagull
也许在美学上它与原始正则表达式很是类似,执行while循环实现。可是如前所述,因为上面提到的许多缘由,这是更好的方法,移除 /g 不会致使无限循环。
如今能够将导入分配给变量:
element.addEventListener('click', async() => { const module = await import(`./api-scripts/button-click.js`); module.clickEvent(); })
扁平化多维数组:
let multi = [1,2,3,[4,5,6,[7,8,9,[10,11,12]]]]; multi.flat(); // [1,2,3,4,5,6,Array(4)] multi.flat().flat(); // [1,2,3,4,5,6,7,8,9,Array(3)] multi.flat().flat().flat(); // [1,2,3,4,5,6,7,8,9,10,11,12] multi.flat(Infinity); // [1,2,3,4,5,6,7,8,9,10,11,12]
let array = [1, 2, 3, 4, 5]; array.map(x => [x, x * 2]); let array = [1, 2, 3, 4, 5]; array.map(x => [x, x * 2]);
结果:
[Array(2), Array(2), Array(2), Array(2), Array(2)] 0: (2) [1, 2] 1: (2) [2, 4] 2: (2) [3, 6] 3: (2) [4, 8] 4: (2) [5, 10]
使用 flatMap
方法:
array.flatMap(v => [v, v * 2]); [1, 2, 2, 4, 3, 6, 4, 8, 5, 10]
将键值对列表转换为对象:
let obj = { apple : 10, orange : 20, banana : 30 }; let entries = Object.entries(obj); entries; (3) [Array(2), Array(2), Array(2)] 0: (2) ["apple", 10] 1: (2) ["orange", 20] 2: (2) ["banana", 30] let fromEntries = Object.fromEntries(entries); { apple: 10, orange: 20, banana: 30 }
let greeting = " Space around "; greeting.trimEnd(); // " Space around"; greeting.trimStart(); // "Space around ";
此更新修复了字符 U+D800 到 U+DFFF 的处理,有时能够进入 JSON 字符串。 这多是一个问题,由于 JSON.stringify 可能会将这些数字格式化为没有等效 UTF-8 字符的值, 但 JSON 格式须要 UTF-8
编码。
解析方法使用格式良好的JSON字符串,如:
'{ “prop1” : 1, "prop2" : 2 }'; // A well-formed JSON format string
注意,要建立正确 JSON 格式的字符串,绝对须要在属性名周围加上双引号。缺乏或任何其余类型的引号都不会生成格式良好的JSON。
'{ “prop1” : 1, "meth" : () => {}}'; // Not JSON format string
JSON 字符串格式与 Object Literal 不一样,后者看起来几乎同样,但可使用任何类型的引号括住属性名,也能够包含方法(JSON格式不容许使用方法):
let object_literal = { property: 1, meth: () => {} };
无论怎样,一切彷佛都很好。第一个示例看起来是兼容的。但它们也是简单的例子,大多数状况下都能顺利地工做!
问题是, ES10 以前的 EcmaScript 实际上并不彻底支持 JSON 格式。前 ES10 时代不接受未转义行分隔符 U+2028 和段落分隔符 U+2029 字符:
若是这些字符潜入 JSON 格式的字符串(假设来自数据库记录),你可能会花费数小时试图弄清楚为何程序的其他部分会产生解析错误。
所以,若是你传递 eval 这样的字符串 “console.log(' hello ')”
,它将执行 JavaScript语句 (经过尝试将字符串转换为实际代码),也相似于 JSON.parse 将处理你的 JSON 字符串的方式。
V8 以前的实现对包含10个以上项的数组使用了一种不稳定的快速排序算法。
一个稳定的排序算法是当两个键值相等的对象在排序后的输出中出现的顺序与在未排序的输入中出现的顺序相同时。
但状况再也不是这样了,ES10 提供了一个稳定的数组排序:
var fruit = [ { name: "Apple", count: 13, }, { name: "Pear", count: 12, }, { name: "Banana", count: 12, }, { name: "Strawberry", count: 11, }, { name: "Cherry", count: 11, }, { name: "Blackberry", count: 10, }, { name: "Pineapple", count: 10, } ]; // 建立排序函数: let my_sort = (a, b) => a.count - b.count; // 执行稳定的ES10排序: let sorted = fruit.sort(my_sort); console.log(sorted);
控制台输出(项目以相反的顺序出现):
函数是对象,而且每一个对象都有一个 .toString() 方法,由于它最初存在于Object.prototype.toString() 上。 全部对象(包括函数)都是经过基于原型的类继承从它继承的。
这意味着咱们之前已经有 funcion.toString() 方法了。
可是 ES10 进一步尝试标准化全部对象和内置函数的字符串表示。 如下是各类新案例:
function () { console.log('Hello there.'); }.toString();
控制台输出(函数体的字符串格式:)
⇨ function () { console.log('Hello there.'); }
下面是剩下的例子:
Number.parseInt.toString(); ⇨ function parseInt() { [native code] }
function () { }.bind(0).toString(); ⇨ function () { [native code] }
Symbol.toString(); ⇨ function Symbol() { [native code] }
function* () { }.toString(); ⇨ function* () { }
Function.prototype.toString.call({}); ⇨ Function.prototype.toString requires that 'this' be a Function"
在过去,try/catch 语句中的 catch 语句须要一个变量。 try/catch 语句帮助捕获终端级别的错误:
try { // Call a non-existing function undefined_Function undefined_Function("I'm trying"); } catch(error) { // Display the error if statements inside try above fail console.log( error ); // undefined_Function is undefined }
在某些状况下,所需的错误变量是未使用的:
try { JSON.parse(text); // <--- this will fail with "text not defined" return true; <--- exit without error even if there is one } catch (redundant_sometmes) <--- this makes error variable redundant { return false; }
编写此代码的人经过尝试强制 true
退出 try 子句。可是,这并非实际发生的状况
(() => { try { JSON.parse(text) return true } catch(err) { return false } })() => false
如今能够跳过错误变量:
try { JSON.parse(text); return true; } catch { return false; }
目前还没法测试上一个示例中的 try 语句的结果,但一旦它出来,我将更新这部分。
这在ES10以前, globalThis 尚未标准化。
在产品代码中,你能够本身编写这个怪物,在多个平台上“标准化”它:
var getGlobal = function () { if (typeof self !== 'undefined') { return self; } if (typeof window !== 'undefined') { return window; } if (typeof global !== 'undefined') { return global; } throw new Error('unable to locate global object'); };
但即便这样也不老是奏效。所以,ES10 添加了 globalThis 对象,从如今开始,该对象用于在任何平台上访问全局做用域:
// 访问全局数组构造函数 globalThis.Array(0, 1, 2); ⇨ [0, 1, 2] // 相似于 ES5 以前的 window.v = { flag: true } globalThis.v = { flag: true }; console.log(globalThis.v); ⇨ { flag: true }
description
是一个只读属性,它返回 Symbol 对象的可选描述。
let mySymbol = 'My Symbol'; let symObj = Symbol(mySymbol); symObj; // Symbol(My Symbol) symObj.description; // "My Symbol"
也就是 unix 用户熟悉的 shebang。它指定一个解释器(什么将执行JavaScript文件?)。
ES10标准化,我不会对此进行详细介绍,由于从技术上讲,这并非一个真正的语言特性,但它基本上统一了 JavaScript 在服务器端的执行方式。
$ ./index.js
代替
$ node index.js
新的语法字符 #octothorpe(hash tag)如今用于直接在类主体的范围内定义变量,函数,getter 和 setter ......以及构造函数和类方法。
下面是一个毫无心义的例子,它只关注新语法:
class Raven extends Bird { #state = { eggs: 10}; // getter get #eggs() { return state.eggs; } // setter set #eggs(value) { this.#state.eggs = value; } #lay() { this.#eggs++; } constructor() { super(); this.#lay.bind(this); } #render() { /* paint UI */ } }
老实说,我认为这会让语言更难读。
代码部署后可能存在的BUG无法实时知道,过后为了解决这些BUG,花了大量的时间进行log 调试,这边顺便给你们推荐一个好用的BUG监控工具 Fundebug。
原文:
https://medium.freecodecamp.o...
你的点赞是我持续分享好东西的动力,欢迎点赞!
干货系列文章汇总以下,以为不错点个Star,欢迎 加群 互相学习。
https://github.com/qq44924588...
我是小智,公众号「大迁世界」做者,对前端技术保持学习爱好者。我会常常分享本身所学所看的干货,在进阶的路上,共勉!
关注公众号,后台回复福利,便可看到福利,你懂的。