日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

ECMAScript 2016,2017,和2018中新增功能

發(fā)布時(shí)間:2024/4/13 编程问答 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ECMAScript 2016,2017,和2018中新增功能 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

ECMAScript 2016

1.Array.property.includes() indexOf()不支持查找NaN,includes支持。

2.7**2 指數(shù)運(yùn)算符,結(jié)果為49

ECMAScript 2017

1.Object.values() 返回Object自身屬性的所有值,排除原型鏈中的所有值。

2.Object.entries() 以數(shù)組方式返回keys和values。

遍歷

const obj = { foo: 'bar', baz: 42 }; console.log(Object.entries(obj)); // [ ['foo', 'bar'], ['baz', 42] ] const obj = { a: 5, b: 7, c: 9 }; for (const [key, value] of Object.entries(obj)) {console.log(`${key} ${value}`); // "a 5", "b 7", "c 9" } Object.entries(obj).forEach(([key, value]) => {console.log(`${key} ${value}`); // "a 5", "b 7", "c 9" }); View Code

生成Map

const obj = { foo: 'bar', baz: 42 }; const a = Object.entries(obj); var b= new Map(a) //?{"foo" => "bar", "baz" => 42} View Code

3.String Padding

向字符串String.proptotype.padStart和String.prototype.padEnd添加了兩個(gè)實(shí)例方法--他們?cè)试S在原始字符串的開始或是結(jié)尾附加、預(yù)先添加空字符串或是其他字符串。

'5'.padStart(10) // ' 5' '5'.padStart(10, '=*') //'=*=*=*=*=5' '5'.padEnd(10) // '5 ' '5'.padEnd(10, '=*') //'5=*=*=*=*=' View Code //ES2017 //如果你有一個(gè)不同長(zhǎng)度的項(xiàng)目列表,并希望格式化它們的顯示目的,你可以使用padStart const formatted = [0, 1, 12, 123, 1234, 12345].map(num => num.toString().padStart(10, '0') // 添加 0 直到長(zhǎng)度為 10 ); console.log(formatted); //打印 // [ // '0000000000', // '0000000001', // '0000000012', // '0000000123', // '0000001234', // '0000012345', // ] View Code const cars = {'?BMW': '10','?Tesla': '5','?Lamborghini': '0' } Object.entries(cars).map(([name, count]) => {//padEnd appends ' -' until the name becomes 20 characters//padStart prepends '0' until the count becomes 3 characters.console.log(`${name.padEnd(20, ' -')} Count: ${count.padStart(3, '0')}`) }); //打印結(jié)果.. // ?BMW - - - - - - - Count: 010 // ?Tesla - - - - - - Count: 005 // ?Lamborghini - - - Count: 000 View Code 'heart'.padStart(10, "??"); 22:10:05.595 "?????heart" View Code

4.Object.getOwnPropertyDescriptors

此方法返回給定對(duì)象的所有屬性的所有詳細(xì)信息(包括?get``set?方法). 添加這個(gè)的主要?jiǎng)訖C(jī)是允許淺拷貝/克隆一個(gè)對(duì)象到另一個(gè)對(duì)象中,這個(gè)對(duì)象也拷貝getter和setter函數(shù),而不是Object.assign。Object.assign淺克隆除原始源對(duì)象的getter和setter函數(shù)以外的所有詳細(xì)信息。

var Car = {name: 'BMW',price: 1000000,set discount(x) {this.d = x;},get discount() {return this.d;}, }; //使用Object.defineProperties將Car的屬性復(fù)制到ElectricCar2 //并使用Object.getOwnPropertyDescriptors提取Car的屬性 const ElectricCar2 = Object.defineProperties({}, Object.getOwnPropertyDescriptors(Car)); //打印ElectricCar2對(duì)象“discount”屬性的詳細(xì)信息 console.log(Object.getOwnPropertyDescriptor(ElectricCar2, 'discount')); //prints.. // { get: [Function: get], // set: [Function: set], // enumerable: true, // configurable: true // } // 請(qǐng)注意,getter和setter存在于ElectricCar2對(duì)象中,用于'discount'屬性! View Code

5.尾隨逗號(hào)

6.Async/Await

//ES2015 Promise function getAmount(userId) {getUser(userId).then(getBankBalance).then(amount => {console.log(amount);}); }****************************//ES2017 async function getAmount2(userId) {var user = await getUser(userId);var amount = await getBankBalance(user);console.log(amount); } function getUser(userId) {return new Promise(resolve => {setTimeout(() => {resolve('john')}, 1000)}); } function getBankBalance(user) {return new Promise((resolve, reject) => {setTimeout(() => {if(user == 'john') {resolve('$1,000');} else {reject(unknown user);}}, 1000)}); } View Code /異步函數(shù)本身返回一個(gè)Promise async function doubleAndAdd(a, b) {a = await doubleAfterlSec(a);b = await doubleAfterlSec(b);return a + b; } //用法 doubleAndAdd(1, 2).then(console.log); function doubleAfterlSec(param) {return new Promise (resolve => {setTimeout(resolve(param * 2), 1000);}); } View Code //異步函數(shù)本身返回一個(gè)Promise async function doubleAndAdd(a, b) {//注意:這邊使用 Promise.all//注意到使用數(shù)組解構(gòu),捕獲結(jié)果[a, b] = await Promise.all([doubleAfterlSec(a), doubleAfterlSec(b)]);return a + b; } //用法 doubleAndAdd(1, 2).then(console.log); function doubleAfterlSec(param) {return new Promise (resolve => {setTimeout(resolve(param * 2), 1000);}); } View Code // 1. 使用 try catch async function doubleAndAdd(a, b) {try {a = await doubleAfterlSec(a);b = await doubleAfterlSec(b);} catch (e) {return NaN; // return something }return a + b; } // 用法 doubleAndAdd('one', 2).then(console.log) //NaN doubleAndAdd(1, 2).then(console.log) // 6 function doubleAfterlSec(param) {return new Promise((resolve, reject) => {setTimeout(function() {let val = param * 2;isNaN(val) ? reject(NaN) : resolve(val);}, 1000);}); } View Code //因?yàn)楫惒?等待返回一個(gè)Promise,我們可以捕捉整個(gè)函數(shù)的錯(cuò)誤 async function doubleAndAdd(a, b) {a = await doubleAfter1Sec(a);b = await doubleAfter1Sec(b);return a + b; } //用法 doubleAndAdd('one', 2) .then(console.log) .catch(console.log); //use "catch" function doubleAfter1Sec(param) {return new Promise((resolve, reject) => {setTimeout(function() {let val = param * 2;isNaN(val) ? reject(NaN) : resolve(val);}, 1000);}); } View Code

2018-06-23? 我嘗試了改寫之前的項(xiàng)目,使用async/await讓我的coding優(yōu)雅了。

ECMAScript 2018

1.共享內(nèi)存和原子

2.標(biāo)記字面量

在標(biāo)記字面量中,可以編寫一個(gè)函數(shù)來接收字符串文字的硬編碼部分,然后從該自定義函數(shù)中返回所需的任何內(nèi)容。

鏈接

?

轉(zhuǎn)載于:https://www.cnblogs.com/Merrys/p/8875662.html

總結(jié)

以上是生活随笔為你收集整理的ECMAScript 2016,2017,和2018中新增功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。