绉嶈崏ECMAScript2021鏂扮壒鎬?/a>
Posted 绔ユ宸?,
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了绉嶈崏ECMAScript2021鏂扮壒鎬?/a>相关的知识,希望对你有一定的参考价值。
瑙傛劅搴︼細馃専馃専馃専馃専馃専
鍙e懗锛氳禌铻冭煿
鐑归オ鏃堕棿锛?min
鏈枃宸叉敹褰曞湪鍓嶇椋熷爞鍚屽悕浠撳簱Github github.com/Geekhyt锛屾杩庡厜涓撮鍫傦紝濡傛灉瑙夊緱閰掕彍杩樼畻鍙彛锛岃祻涓?Star 瀵归鍫傝€佹澘鏉ヨ鏄帿澶х殑榧撳姳銆?/blockquote>ECMAScript
ECMAScript
鏄?Ecma International 棰佸竷鐨勪竴閮ㄨ瑷€鏍囧噯锛岀紪鍙蜂负 262锛屽張绉颁负ECMA-262
銆?/p>
Ecma International
鍒欐槸涓€涓埗瀹氫俊鎭拰閫氳鎶€鏈柟闈㈢殑鍥介檯鏍囧噯鐨勭粍缁囷紝鍓嶈韩鏄娲茶绠楁満鍒堕€犲晢鍗忎細(European Computer Manufacturers Association
)锛岄殢鐫€璁$畻鏈虹殑鍥介檯鍖栵紝鏈烘瀯鍚嶇О鏀逛负鍏惰嫳鏂囧崟璇嶉瀛楁瘝缂╁啓銆?/p>
ECMAScript
鐢?EMCA International
鐨?TC39锛?code>Technical Committee 39锛夋妧鏈鍛樹細缂栧啓銆?/p>
TC39
浼氬皢缂栧啓瀹屾垚鐨?ECMAScript
鏍囧噯鏂囨。鎻愪氦缁?Ecma International
锛屽苟鐢卞叾姝e紡鍙戝竷銆?/p>浠?2015 骞村紑濮嬶紝
ECMAScript
姣忓勾閮戒細鍙戝竷涓€涓寮忕増锛屽苟鍦ㄦ爣棰樹腑鍐欎笂骞翠唤锛屾瘮濡?code>銆孍CMAScript庐 2020 Language Specification, 11th edition銆?/code>锛屽彲绠€绉颁负ES2020
鎴?code>ES11銆?/p>浜斾釜娴佺▼闃舵
濡傛灉鎯宠鏂板鎴栨槸鏀瑰啓瑙勮寖锛屼竴鑸缁忓巻5涓樁娈碉紝濡?a href="https://link.segmentfault.com/?url=https%3A%2F%2Ftc39.es%2Fprocess-document%2F" rel="nofollow">TC39 Process涓墍绀猴細
- Strawperson
- Proposal
- Draft
- Candidate
- Finished
缁忓巻杩囪繖5涓樁娈碉紝杩涘叆 Finished 鐘舵€佺殑淇敼鎵嶄細琚垪鍏ユ寮忕増鐨勮鑼冦€?/p>
ECMAScript2021
https://github.com/tc39/proposals/blob/master/finished-proposals.md
浜嗚В浜?
ECMAScript
锛屼笅闈㈠氨杩涘叆姝f枃锛岃鎴戜滑鏉ョ湅鐪嬪凡缁忕‘瀹氱殑 ECMAScript2021 鐨勬柊鐗规€у惂銆?/p>1.String.prototype.replaceAll
https://github.com/tc39/proposal-string-replaceall
鍏堟潵鍥為【涓?
String.prototype.replace
鐨勭敤娉曪細const str = \'Stay Hungry. Stay Foolish.\' const newStr = str.replace(\'Stay\', \'Always\') console.log(newStr) // Always Hungry. Stay Foolish.
濡傛灉鎴戜滑杩欐牱鍐欙紝鍙湁绗竴涓尮閰嶇殑浼氳鏇挎崲銆?/p>
鎯宠鍋氬埌鍏ㄩ儴鏇挎崲灏遍渶瑕佷娇鐢ㄦ鍒欒〃杈惧紡銆?/p>
const str = \'Stay Hungry. Stay Foolish.\' const newStr = str.replace(/Stay/g, \'Always\') console.log(newStr) // Always Hungry. Always Foolish.
涓嶈繃鍦ㄤ娇鐢ㄦ鍒欑殑鏃跺€欙紝濡傛灉闇€姹傛槸鍖归厤
+
绛夌鍙锋椂锛岃繕闇€瑕佽繘琛岃浆涔夈€傚锛?/p>
/\\+/g
鑱槑鐨勪綘涔熻浼氭兂鍒板彟澶栦竴绉嶆柟妗堬細浣跨敤
split + join
鐨勬柟寮?/p>杩欓噷鍊熺敤涓嬪畼鏂圭殑渚嬪瓙锛?/p>
const queryString = \'q=query+string+parameters\'; const withSpaces = queryString.split(\'+\').join(\' \'); // q=query string parameters
浣嗚繖鏍峰仛涔熸槸鏈夋€ц兘寮€閿€鐨勶紝鍔犱笂杩欑鎿嶄綔鍗佸垎甯歌銆備簬鏄氨璇炵敓浜?
String.prototype.replaceAll
杩欎釜 API锛屾垜浠彲浠ユ洿鍔犳柟渚跨殑鏉ヨ繘琛屾搷浣溿€?/p>const str = \'Stay Hungry. Stay Foolish.\' const newStr = str.replaceAll(\'Stay\', \'Always\') console.log(newStr) // Always Hungry. Always Foolish.
String.prototype.replaceAll(searchValue, replaceValue)
娉ㄦ剰锛氬綋
searchValue
鏄潪鍏ㄥ眬姝e垯琛ㄨ揪寮忔椂锛?code>replaceAll 浼氬紩鍙戝紓甯搞€傚鏋?searchValue
鏄叏灞€姝e垯琛ㄨ揪寮忔椂锛?code>replaceAll 涓?replace
琛屼负鏄竴鑷寸殑銆?/p>2.Promise.any
https://github.com/tc39/proposal-promise-any
const p = Promise.all([p1, p2, p3]);
Promise.all
(ES2015) 鍙湁褰撲紶鍏ョ殑姣忎釜 Promise 瀹炰緥(p1,p2,p3)鐨勭姸鎬侀兘鍙樻垚 fulfilled 鏃讹紝p 鎵?fulfilled锛屽彧瑕?p1,p2,p3)鏈変竴涓 rejected锛宲 鐨勭姸鎬佸氨鍙樻垚 rejected銆?/li>Promise.race
(ES2015) 褰撲紶鍏ョ殑 Promise 瀹炰緥(p1,p2,p3)涓湁涓€涓巼鍏堟敼鍙樼姸鎬侊紝閭d箞 p 鐨勭姸鎬佸氨璺熺潃鏀瑰彉锛屼篃灏辨槸璇磋繑鍥炴渶鍏堟敼鍙樼殑 Promise 瀹炰緥鐨勮繑鍥炲€笺€?/li>Promise.allSettled
(ES2020) 鍙湁绛夊埌鎵€鏈変紶鍏ョ殑 Promise 瀹炰緥(p1,p2,p3)閮借繑鍥炵粨鏋滐紝涓嶇鏄?fulfilled 杩樻槸 rejected锛屽寘瑁呭疄渚嬫墠浼氱粨鏉熴€?/li>Promise.any
(ES2021) 褰撳叾涓换浣曚竴涓?Promise 瀹屾垚(fulfilled)鏃讹紝灏辫繑鍥為偅涓凡缁忔湁瀹屾垚鍊肩殑 Promise銆傚鏋滄墍鏈夌殑 Promise 閮芥嫆缁?(rejected), 閭d箞杩斿洖涓€涓嫆缁濈殑 Promise銆?/li>瀵规瘮璁板繂
- 鎴戜滑鍙互鎶?
Promise.any()
鐞嗚В鎴?Promise.all()
鐨勫弽鍚戞搷浣溿€?/li>鑷存暚闊︾锛?/strong>
Promise.any()
璺?Promise.race()
鏂规硶寰堝儚锛屾湁涓€涓笉鍚岀偣鏄細鍓嶈€呬笉浼氬洜涓烘煇涓?Promise
鍙樻垚rejected
鐘舵€佽€岀粨鏉熴€?/li>鎯宠浜嗚В鏇村缁嗚妭鍙互鐪嬮槷鑰佸笀鐨凟CMAScript 6 鍏ラ棬
Promise.any(promises).then( (first) => { // 浠讳綍涓€涓狿romise瀹屾垚 }, (error) => { // 鎵€鏈夌殑 Promise閮芥嫆缁濅簡 } )
any 鍚嶅瓧鐨勭敱鏉?/h3>
any 椤惧悕鎬濅箟锛屼笉浠呮竻妤氱殑鎻忚堪浜嗗畠鐨勪綔鐢紝鑰屼笖鍦ㄦ彁渚涙鍔熻兘鐨勭涓夋柟搴撲腑閮芥槸杩欐牱鍛藉悕鐨勶紝鐢ㄨ繃鐨勫悓瀛︿滑涓€瀹氳寰楀緢浜插垏銆?/p>
- https://github.com/kriskowal/q#combination
- http://bluebirdjs.com/docs/api/promise.any.html
- https://github.com/m0ppers/promise-any
- https://github.com/cujojs/when/blob/master/docs/api.md#whenany
- https://github.com/sindresorhus/p-any
鍙瑙佺殑浣滅敤
瀹樻柟鎻愪緵浜嗕竴涓緥瀛愶紝鍙互搴旂敤
Promise.any()
妫€鏌ュ摢涓珯鐐硅闂渶蹇€?/p>Promise.any([ fetch(\'https://v8.dev/\').then(() => \'home\'), fetch(\'https://v8.dev/blog\').then(() => \'blog\'), fetch(\'https://v8.dev/docs\').then(() => \'docs\') ]).then((first) => { // Any of the promises was fulfilled. console.log(first); // 鈫?\'home\' }).catch((error) => { // All of the promises were rejected. console.log(error); });
3.WeakRefs
https://github.com/tc39/proposal-weakrefs
娉ㄦ剰锛氳灏介噺閬垮厤浣跨敤
WeakRef
鍜?FinalizationRegistry
锛屽瀮鍦惧洖鏀舵満鍒朵緷璧栦簬 JavaScript 寮曟搸鐨勫疄鐜帮紝涓嶅悓鐨勫紩鎿庢垨鏄笉鍚岀増鏈殑寮曟搸鍙兘浼氭湁鎵€涓嶅悓銆?/p>杩欎釜鎻愭涓昏鍖呮嫭涓や釜涓昏鐨勬柊鍔熻兘锛?/p>
- 浣跨敤
WeakRef
绫诲垱寤哄璞$殑寮卞紩鐢?/li>- 浣跨敤
FinalizationRegistry
绫诲瀵硅薄杩涜鍨冨溇鍥炴敹鍚庯紝杩愯鐢ㄦ埛瀹氫箟鐨勭粓缁撳櫒瀹冧滑鍙互鍒嗗紑浣跨敤涔熷彲浠ヤ竴璧蜂娇鐢ㄣ€?/p>
WeakRef
瀹炰緥涓嶄細闃绘 GC 鍥炴敹锛屼絾鏄?GC 浼氬湪涓ゆEventLoop
涔嬮棿鍥炴敹WeakRef
瀹炰緥銆?code>GC 鍥炴敹鍚庣殑WeakRef
瀹炰緥鐨?deref()
鏂规硶灏嗕細杩斿洖undefined
銆?/p>let ref = new WeakRef(obj) let isLive = ref.deref() // 濡傛灉 obj 琚瀮鍦惧洖鏀朵簡锛岄偅涔?isLive 灏辨槸 undefined
FinalizationRegistry
娉ㄥ唽Callback
锛屾煇涓璞¤ GC 鍥炴敹鍚庤皟鐢ㄣ€?/p>const registry = new FinalizationRegistry(heldValue => { // .... }); // 閫氳繃 register 娉ㄥ唽浠讳綍浣犳兂瑕佹竻鐞嗗洖璋冪殑瀵硅薄锛屼紶鍏ヨ瀵硅薄鍜屾墍鍚殑鍊?registry.register(theObject, "some value");
鍏充簬鏇村鐨勭粏鑺備綘鍙互鏌ラ槄锛?/p>
- https://github.com/tc39/proposal-weakrefs/blob/master/reference.md
4.Logical Assignment Operators 閫昏緫璧嬪€兼搷浣滅
https://github.com/tc39/proposal-logical-assignment
鍏堟潵鍥為【涓?
ES2020
鏂板鐨勭┖鍊煎悎骞舵搷浣滅??
鍦ㄥ綋宸︿晶鎿嶄綔鏁颁负
undefined
鎴?null
鏃讹紝璇ユ搷浣滅浼氬皢鍙充晶鎿嶄綔鏁拌祴鍊肩粰宸︿晶鍙橀噺銆?/p>const name = null ?? \'鍓嶇椋熷爞\' console.log(name) // 鍓嶇椋熷爞
鏈変簡閫昏緫璧嬪€艰繍绠楃锛屾垜浠彲浠ユ浛鎹㈡帀濡備笅鏃х殑鍐欐硶锛?/p>
const demo = function() { // 鏃х殑鍐欐硶1 // if (!a) { // a = \'瑗跨摐\' // } // 鏃х殑鍐欐硶2 // a = a || \'瑗跨摐\' // 鏂扮殑鍐欐硶 a ||= \'瑗跨摐\' } a ||= b; // 绛夊悓浜?a || (a = b); a &&= b; // 绛夊悓浜?a && (a = b); a ??= b; // 绛夊悓浜?a ?? (a = b);
5.Numeric separators 鏁板瓧鍒嗛殧绗?/h2>
https://github.com/tc39/proposal-numeric-separator
鏁板瓧鐨勫彲璇绘€ч殢鐫€鏁板瓧鍙橀暱鑰屽彉宸紝鏁板瓧鍒嗛殧绗︿細璁╅暱鏁板瓧鏇村姞娓呮櫚銆?/p>
const x = 1000000000000 const y = 1_000_000_000_000 console.log(x === y) // true
鍦ㄤ簩杩涘埗銆佸崄鍏繘鍒躲€丅igInt 绛変腑閮藉彲浠ヤ娇鐢ㄣ€?/p>
鉂わ笍鐖卞績涓夎繛鍑?/h2>
1.濡傛灉浣犺寰楅鍫傞厭鑿滆繕鍚堣儍鍙o紝灏辩偣涓禐鏀寔涓嬪惂锛屼綘鐨?strong>璧?/strong>鏄垜鏈€澶х殑鍔ㄥ姏銆?/p>
2.鍏虫敞鍏紬鍙峰墠绔鍫傦紝鍚冨ソ姣忎竴椤块キ锛?/strong>
3.鐐硅禐銆佽瘎璁恒€佽浆鍙?=== 鍌洿锛?/p>
以上是关于绉嶈崏ECMAScript2021鏂扮壒鎬?/a>的主要内容,如果未能解决你的问题,请参考以下文章
鍏ㄧ綉棣栧彂锛侊紒C++20鏂扮壒鎬у叏鍦ㄨ繖涓€寮犲浘閲屼簡