[绉戞櫘]ES6涓€浜涗笉甯歌鐨勫皬鐭ヨ瘑
Posted 鍓嶇LeBron
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[绉戞櫘]ES6涓€浜涗笉甯歌鐨勫皬鐭ヨ瘑相关的知识,希望对你有一定的参考价值。
鍐欎綔涓嶆槗锛屾湭缁忎綔鑰呭厑璁哥姝互浠讳綍褰㈠紡杞浇锛?br>濡傛灉瑙夊緱鏂囩珷涓嶉敊锛屾杩庡叧娉ㄣ€佺偣璧炲拰鍒嗕韩锛?br>鎸佺画鍒嗕韩鎶€鏈崥鏂囷紝鍏虫敞寰俊鍏紬鍙仿狆煈夝煆?鍓嶇LeBron
鎺橀噾鍘熸枃
WeakMap
鍓嶇疆鐭ヨ瘑[娣卞叆娴呭嚭]javascript GC 鍨冨溇鍥炴敹鏈哄埗
浠€涔堟槸WeakMap锛?/h2>WeakMap鏄痥ey / value鐨勭粍鍚堬紝key鍙帴鍙楀璞★紝涓嶆帴鍙楀熀鏈被鍨嬶紝value鍙互涓轰换鎰忕被鍨嬨€?/blockquote>鏂规硶
- set(key, value)
鍦╓eakMap涓缃竴缁勫叧鑱斿璞★紝杩斿洖WeakMap瀵硅薄
- get(key)
杩斿洖key鐨勫叧鑱斿璞★紝涓嶅瓨鍦ㄦ椂杩斿洖undefined
- has(key)
鏍规嵁鏄惁鏈塳ey鍏宠仈瀵硅薄锛屾斁鍥炰竴涓狟oolean鍊?/blockquote>- delete(key)
绉婚櫎key鐨勫叧鑱斿璞★紝涔嬪悗鎵цhas(key)鏂规硶杩斿洖false
鍜孧ap鏈変粈涔堝尯鍒紵
- Map鐨刱ey / value锛岄兘鍙互鏄换鎰忕被鍨?/li>
- WeakMap鐨刱ey鍙兘鏄璞★紝value鍙互鏄换鎰忕被鍨?/li>
const name = "LeBron";
const person = {
name: "LeBron",
age: 21,
};
let wk = new WeakMap();
wk.set(person, "nice");
console.log(wk.get(person)); // nice
wk.set(name, 1); // TypeError: Invalid value used as weak map key
let map = new Map();
map.set(name, "JS");
map.set(person, "nice");
console.log(map.get(name)); // JS
console.log(map.get(person)); // nice
- Map鐨刱ey / value 鏄彲閬嶅巻鐨勶紝鍥犱负瀹冪殑 key / value 瀛樻斁鍦ㄤ竴涓暟缁勪腑銆?/li>
- WeakMap涓嶅瓨鍦ㄥ瓨鏀?key / value 鐨勬暟缁勶紝鎵€浠ヤ笉鍙亶鍘嗐€?/li>
const name = "LeBron";
const person = {
name: "LeBron",
age: 21,
};
let wk = new WeakMap();
wk.set(name, "JS");
wk.set(person, "nice");
console.log(wk.keys()); // TypeError: wk.keys is not a function
console.log(wk.values()); // TypeError: wk.values is not a function
console.log(wk.entries()); // TypeError: wk.entries is not a function
let map = new Map();
map.set(person, "nice");
console.log(map.keys()); // [Map Iterator] { \'LeBron\', { name: \'LeBron\', age: 21 } }
console.log(map.values()); // [Map Iterator] { \'JS\', \'nice\' }
console.log(map.entries()); // [Map Entries] {
// [ \'LeBron\', \'JS\' ],
// [ { name: \'LeBron\', age: 21 }, \'nice\' ]
// }
- Map瀵归敭杩涜寮哄紩鐢紝鍗充娇閿鏍囪涓簄ull浜嗭紝鐢变簬Map鐨刱ey / value鏁扮粍杩樺湪寮曠敤锛宬ey涓嶄細琚獹C銆?/li>
- WeakMap瀵筴ey杩涜寮卞紩鐢紝鍦╧ey琚爣璁颁负null浜嗕互鍚庛€傜敱浜庢槸寮卞紩鐢紝涔熶笉瀛樺湪key / value鏁扮粍寮曠敤锛屼笉褰卞搷key鐨凣C銆?/li>
- 浠ヤ笅绋嬪簭闇€瑕佹墜鍔℅C 鍚姩鏂规硶锛?code>node --expose-gc xxx
Map
function memmorySizeLogger() {
global.gc();
const used = process.memoryUsage().heapUsed;
console.log((used / 1024 / 1024).toFixed(2) + "M");
}
memmorySizeLogger(); // 1.79M
let person = {
name: "LeBron",
age: 21,
tmp: new Array(5 * 1024 * 1024),
};
memmorySizeLogger(); // 41.96M
let map = new Map();
memmorySizeLogger(); // 41.96M
map.set(person, "nice");
memmorySizeLogger(); // 41.96M
person = null;
memmorySizeLogger(); // 41.96M person鐨勫唴瀛樻病鏈夎鍥炴敹
濡傛灉鎯冲湪杩欑鎯呭喌涓嬫甯窯C锛屾爣璁颁负null鍓嶉渶鍏堟墽琛宮ap.delete(person)
WeakMap
function memmorySizeLogger() {
global.gc();
const used = process.memoryUsage().heapUsed;
console.log((used / 1024 / 1024).toFixed(2) + "M");
}
memmorySizeLogger(); // 1.79M
let person = {
name: "LeBron",
age: 21,
tmp: new Array(5 * 1024 * 1024),
};
memmorySizeLogger(); // 41.96M
let wk = new WeakMap();
memmorySizeLogger(); // 41.96M
wk.set(person, "nice");
memmorySizeLogger(); // 41.96M
person = null;
memmorySizeLogger(); // 1.96M person鐨勫唴瀛樿鍥炴敹
Why WeakMap锛?/h2>鍦↗S閲岀殑Map API鍏辩敤涓や釜鏁扮粍锛坘ey銆乿alue锛夛紝璁剧疆鐨刱ey銆乿alue閮戒細鍔犲埌杩欎袱涓暟缁勭殑鏈熬锛屽苟瀵筴ey浜х敓寮曠敤銆傚綋浠巑ap鍙栧€兼椂锛岄渶瑕侀亶鍘嗘墍鏈夌殑key锛岀劧鍚庨€氳繃绱㈠紩浠巚alue鏁扮粍涓彇鍑虹浉搴攊ndex鐨勫€笺€?/p>
缂虹偣涓€锛?/p>
璧嬪€笺€佹悳绱㈤兘鏄疧(n)澶嶆潅搴?/p>
缂虹偣浜岋細
浣跨敤Map瀹规槗鍑虹幇鍐呭瓨娉勬紡锛屽洜涓烘暟缁勪竴鐩村紩鐢ㄧ潃姣忎釜key鍜寁alue锛屽鑷存棤娉曟甯窯C銆?/p>
WeakMap瀵筴ey杩涜寮卞紩鐢紝涓嶅奖鍝嶆甯窯C
- key琚獹C鍚庡け鏁?/li>
濡傛灉浣犺寰€瀵硅薄涓婃坊鍔犳暟鎹紝鍙堜笉鎯冲共鎵板瀮鍦惧洖鏀舵満鍒讹紝灏卞彲浠ヤ娇鐢?WeakMap
- 濡傛灉闇€瑕侀亶鍘?/ 杩唬锛屽垯闇€瑕佷娇鐢∕ap
搴旂敤鍦烘櫙
淇濆瓨DOM鑺傜偣鏁版嵁
let domData = new WeakMap();
let dom = document.getElementById("xxx");
const anyDomData = getDomData(dom);
domData.set(dom, anyDomData);
console.log(domData.get(dom));
dom.parentNode.removeChild(dom);
dom = null;
缂撳瓨鐩稿叧鏁版嵁
let cache = new WeakMap();
class HandleCache {
get(key) {
if (cache.has(key)) {
return cache.get(key);
} else {
return undefined;
}
}
set(key, value) {
cache.set(key, value)
}
delete(key){
cache.delete(key)
}
}
灏佽绉佹湁灞炴€?/h3>let privateData = new WeakMap();
class Person{
constructor(name, age){
privateData.set(this,{name, age});
}
getData(){
return privateData.get(this);
}
}
WeakSet
鍓嶇疆鐭ヨ瘑[娣卞叆娴呭嚭]JavaScript GC 鍨冨溇鍥炴敹鏈哄埗
浠€涔堟槸WeakSet
WeakSet瀵硅薄鏄竴浜涘璞″€肩殑闆嗗悎锛屽苟涓斿叾涓殑姣忎釜瀵硅薄鍙兘鍑虹幇涓€娆★紝鍦╓eakSet闆嗗悎涓槸鍞竴鐨?/blockquote>鏂规硶
- add(value)
鍦ㄨWeakSet瀵硅薄涓坊鍔犱竴涓柊鐨勫厓绱爒alue
- delete(value)
鍦ㄨWeakSet瀵硅薄涓垹闄alue杩欎釜鍏冪礌鍚庯紝has鏂规硶浼氳繑鍥瀎alse銆?/blockquote>- has(value)
杩斿洖涓€涓狟oolean鍊硷紝琛ㄧず缁欏畾鐨剉alue鍊兼槸鍚﹀瓨鍦ㄨ繖涓猈eakSet涓?/blockquote>鍜孲et鏈変粈涔堝尯鍒?/h2>- Set鐨剉alue鍙互鏄换浣曞€硷紝WeakSet鐨勫€煎彧鑳芥槸瀵硅薄
const name = "LeBron";
const age = 21;
const person = {
name: "LeBron",
age: 21,
};
const ws = new WeakSet();
const set = new Set();
set.add(name);
set.add(age);
set.add(person);
ws.add(person);
ws.add(name); // TypeError: Invalid value used in weak set
ws.add(age); // TypeError: Invalid value used in weak set
- Set鏄彲閬嶅巻鐨勶紝WeakSet涓嶅彲閬嶅巻
- Set瀛樺湪涓€涓暟缁勫瓨鏀緑alue鐨勫€硷紝寮曠敤鍘熷璞★紝鏁呭彲閬嶅巻
- WeakSet涓嶅瓨杩欐牱鐨勬暟缁勶紝鏁呬笉鍙亶鍘?/li>
const name = "LeBron";
const age = 21;
const person = {
name: "LeBron",
age: 21,
};
const ws = new WeakSet();
const set = new Set();
set.add(name);
set.add(age);
set.add(person);
console.log(set.values()); // { \'LeBron\', 21, { name: \'LeBron\', age: 21 } }
ws.add(person);
ws.add(name);
ws.add(age);
console.log(set.values()); // TypeError: ws.values is not a function
- Set褰卞搷GC锛岃€學eakSet涓嶅奖鍝?/li>
- 浠ヤ笅绋嬪簭闇€瑕佹墜鍔℅C 鍚姩鏂规硶锛?code>node --expose-gc xxx
Set瀛樺湪values鏁扮粍锛屽湪鍘焩alue鎸囧悜null鍚庯紝values鏁扮粍浠嶅value鐨勫€煎瓨鍦ㄥ己寮曠敤锛屽奖鍝嶆甯窯C
function memmorySizeLogger() {
global.gc();
const used = process.memoryUsage().heapUsed;
console.log((used / 1024 / 1024).toFixed(2) + "M");
}
memmorySizeLogger(); // 1.79M
let person = {
name: "LeBron",
age: 21,
tmp: new Array(5 * 1024 * 1024),
};
memmorySizeLogger(); // 41.96M
const set = new Set();
set.add(person);
memmorySizeLogger(); // 41.96M
person = null;
memmorySizeLogger(); // 41.96M
WeakSet涓嶅瓨鍦ㄨ繖鏍风殑鏁扮粍锛屾晠涓嶅奖鍝嶆甯窯C
function memmorySizeLogger() {
global.gc();
const used = process.memoryUsage().heapUsed;
console.log((used / 1024 / 1024).toFixed(2) + "M");
}
memmorySizeLogger(); // 1.79M
let person = {
name: "LeBron",
age: 21,
tmp: new Array(5 * 1024 * 1024),
};
memmorySizeLogger(); // 41.96M
const ws = new WeakSet();
ws.add(person);
memmorySizeLogger(); // 41.96M
person = null;
memmorySizeLogger(); // 1.96M
搴旂敤鍦烘櫙
妫€娴嬪惊鐜紩鐢?/h3>閫掑綊璋冪敤鑷韩鐨勫嚱鏁伴渶瑕佷竴绉嶉€氳繃璺熻釜鍝簺瀵硅薄宸茶澶勭悊锛屾潵搴斿寰幆鏁版嵁缁撴瀯鐨勬柟娉?/blockquote>// 瀵?浼犲叆鐨剆ubject瀵硅薄 鍐呴儴瀛樺偍鐨勬墍鏈夊唴瀹规墽琛屽洖璋?function execRecursively(fn, subject, _refs = null){
if(!_refs)
_refs = new WeakSet();
// 閬垮厤鏃犻檺閫掑綊
if(_refs.has(subject))
return;
fn(subject);
if("object" === typeof subject){
_refs.add(subject);
for(let key in subject)
execRecursively(fn, subject[key], _refs);
}
}
const foo = {
foo: "Foo",
bar: {
bar: "Bar"
}
};
foo.bar.baz = foo; // 寰幆寮曠敤!
execRecursively(obj => console.log(obj), foo);
Reflect
Reflect璇戜负鍙嶅皠锛屾槸涓€涓唴缃殑鏂扮殑鍏ㄥ眬瀵硅薄锛屽畠鎻愪緵鎷︽埅JavaScript鎿嶄綔鐨勬柟娉曘€傝繖浜涙柟娉曚笌Proxy handler鐨勬柟娉曠浉鍚屻€俁eflect涓嶆槸涓€涓嚱鏁板璞★紝鏄潤鎬佺殑绫讳技宸ュ叿鍑芥暟锛岀被浼糓ath锛屽洜姝ゅ畠鏄笉鍙瀯閫犵殑
Reflect鐨勯潤鎬佹柟娉?/h2>鍏蜂綋鐢ㄦ硶鍙傝€冿細Reflect MDN鏂囨。
Reflect.apply()
Reflect.construct()
Reflect.defineProperty()
Reflect.deleteProperty()
Reflect.get()
Reflect.getOwnPropertyDescriptor()
Reflect.getPrototypeOf()
Reflect.has()
Reflect.isExtensible()
Reflect.ownKeys()
Reflect.preventExtensions()
Reflect.set()
Reflect.setPrototypeOf()
杩欎簺鏂规硶涓嶱roxy handler鐨勬柟娉曠殑鍛藉悕鐩稿悓锛屽叾涓殑涓€浜涙柟娉曚笌Object鐨勬柟娉曠浉鍚岋紝灏界浜岃€呬箣闂村瓨鍦ㄧ潃鏌愪簺缁嗗井鐨勫樊鍒?/blockquote>鏈変粈涔堜笉鍚岋紵
Reflect鐨勯潤鎬佹柟娉曡繘琛岀浉搴旀搷浣滀細杩斿洖涓€涓狟oolean鍊?/p>
- 鎿嶄綔鎴愬姛杩斿洖true
- 鎿嶄綔澶辫触杩斿洖false
灏嗗父瑙勭殑鍛戒护寮忔搷浣滆浆鎹㈡垚浜嗗嚱鏁板紡鎿嶄綔锛岀紪绋嬫柟寮忓鍔犱簡鍏冪紪绋嬨€?/p>
- 渚嬪delete銆佽祴鍊笺€佸垽鏂瓑
- 鍛戒护寮忔搷浣滃け璐ヤ竴鑸細鎶ラ敊锛岃€孯eflect涓嶄細锛岃繑鍥炰竴涓狟oolean鍊煎垽鏂槸鍚︽垚鍔熴€?/li>
- 鍐呯疆瀵硅薄涓凡缁忓瓨鍦ㄤ簡涓€浜涘弽灏凙PI锛孯eflect灏嗕粬浠仛鍚堣捣鏉ュ苟杩涜浜嗕紭鍖?/li>
浠€涔堟槸鍏冪紪绋嬶紵
- 鍏冪紪绋嬪嵆瀵圭紪绋嬭瑷€杩涜缂栫▼
- 渚嬪Proxy瀵硅薄鍙互杩涜浠g悊锛屾嫤鎴猤et銆乻et鎿嶄綔
- 鑰屽湪绋嬪簭涓幏鍙栫殑鏄綘缂栫▼鍚庣殑鍊笺€?/li>
Reflect灏辨槸涓€绉嶅弽灏勶紝璋冪敤鐨勬槸澶勭悊杩囧悗鐨勫悇鍐呯疆瀵硅薄涓婄殑鏂规硶
- 鎵€浠ュ悇鍐呯疆瀵硅薄鐨勬柟娉曟敼鍙樺悗锛孯eflect璋冪敤鐨勬柟娉曚篃鏄敼鍙樹簡鐨?/li>
- 绫讳技浜庡皝瑁呬簡涓€灞?/li>
Reflect鐨勪紭鐐?/h2>- 浼樺寲鍛藉悕绌洪棿
浣犱細鍙戠幇JS鐨勫唴缃弽灏勬柟娉曟暎钀藉湪鍚勫锛孯eflect灏嗕粬浠緢濂藉湴缁勭粐浜嗚捣鏉ャ€?/p>
- 澧炲己浠g爜鐨勫仴澹€?/li>
浣跨敤Reflect杩涜鎿嶄綔涓嶅鏄撴姏鍑哄紓甯搞€佺嚎绋嬮樆濉烇紝浣夸唬鐮佹洿鍋ュ.鍦拌繍琛屻€?/p>
- 涓轰粈涔堜笉鐩存帴鎸傚湪Object涓婏紵
鍙嶅皠鐨勫璞′笉浠呴拡瀵逛簬Object锛岃繕鍙兘閽堝鍑芥暟
- 渚嬪apply锛岃皟鐢∣bject.apply(myFunc)杩樻槸鎸哄鎬殑
鐢ㄤ竴涓崟涓€鐨勫璞′繚瀛樺唴缃柟娉曡兘澶熶繚璇丣avaScript浠g爜鍏朵粬瀵硅薄鐨勭函鍑€鎬?/p>
- 杩欐牱瑕佷紭浜庣洿鎺ュ弽灏勬寕杞藉埌鏋勯€犲嚱鏁版垨鑰呭師褰笂
- 鏇翠紭浜庣洿鎺ヤ娇鐢ㄥ叏灞€鍙橀噺锛岃繖鏍稪S鍏抽敭瀛楀皢瓒婃潵瓒婂銆?/li>
- 鎺橀噾鍘熸枃
- 鎺橀噾锛?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fjuejin.cn%2Fuser%2F3913917127467805" rel="nofollow">鍓嶇LeBron
- 鐭ヤ箮锛?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fwww.zhihu.com%2Fpeople%2Flebronchao" rel="nofollow">鍓嶇LeBron
- 鎸佺画鍒嗕韩鎶€鏈崥鏂囷紝鍏虫敞寰俊鍏紬鍙仿狆煈夝煆?鍓嶇LeBron
以上是关于[绉戞櫘]ES6涓€浜涗笉甯歌鐨勫皬鐭ヨ瘑的主要内容,如果未能解决你的问题,请参考以下文章
鏂规硶
- set(key, value)
鍦╓eakMap涓缃竴缁勫叧鑱斿璞★紝杩斿洖WeakMap瀵硅薄
- get(key)
杩斿洖key鐨勫叧鑱斿璞★紝涓嶅瓨鍦ㄦ椂杩斿洖undefined
- has(key)
鏍规嵁鏄惁鏈塳ey鍏宠仈瀵硅薄锛屾斁鍥炰竴涓狟oolean鍊?/blockquote>
- delete(key)
绉婚櫎key鐨勫叧鑱斿璞★紝涔嬪悗鎵цhas(key)鏂规硶杩斿洖false鍜孧ap鏈変粈涔堝尯鍒紵
- Map鐨刱ey / value锛岄兘鍙互鏄换鎰忕被鍨?/li>
- WeakMap鐨刱ey鍙兘鏄璞★紝value鍙互鏄换鎰忕被鍨?/li>
const name = "LeBron"; const person = { name: "LeBron", age: 21, }; let wk = new WeakMap(); wk.set(person, "nice"); console.log(wk.get(person)); // nice wk.set(name, 1); // TypeError: Invalid value used as weak map key let map = new Map(); map.set(name, "JS"); map.set(person, "nice"); console.log(map.get(name)); // JS console.log(map.get(person)); // nice
- Map鐨刱ey / value 鏄彲閬嶅巻鐨勶紝鍥犱负瀹冪殑 key / value 瀛樻斁鍦ㄤ竴涓暟缁勪腑銆?/li>
- WeakMap涓嶅瓨鍦ㄥ瓨鏀?key / value 鐨勬暟缁勶紝鎵€浠ヤ笉鍙亶鍘嗐€?/li>
const name = "LeBron"; const person = { name: "LeBron", age: 21, }; let wk = new WeakMap(); wk.set(name, "JS"); wk.set(person, "nice"); console.log(wk.keys()); // TypeError: wk.keys is not a function console.log(wk.values()); // TypeError: wk.values is not a function console.log(wk.entries()); // TypeError: wk.entries is not a function let map = new Map(); map.set(person, "nice"); console.log(map.keys()); // [Map Iterator] { \'LeBron\', { name: \'LeBron\', age: 21 } } console.log(map.values()); // [Map Iterator] { \'JS\', \'nice\' } console.log(map.entries()); // [Map Entries] { // [ \'LeBron\', \'JS\' ], // [ { name: \'LeBron\', age: 21 }, \'nice\' ] // }
- Map瀵归敭杩涜寮哄紩鐢紝鍗充娇閿鏍囪涓簄ull浜嗭紝鐢变簬Map鐨刱ey / value鏁扮粍杩樺湪寮曠敤锛宬ey涓嶄細琚獹C銆?/li>
- WeakMap瀵筴ey杩涜寮卞紩鐢紝鍦╧ey琚爣璁颁负null浜嗕互鍚庛€傜敱浜庢槸寮卞紩鐢紝涔熶笉瀛樺湪key / value鏁扮粍寮曠敤锛屼笉褰卞搷key鐨凣C銆?/li>
- 浠ヤ笅绋嬪簭闇€瑕佹墜鍔℅C 鍚姩鏂规硶锛?code>node --expose-gc xxx
Mapfunction memmorySizeLogger() { global.gc(); const used = process.memoryUsage().heapUsed; console.log((used / 1024 / 1024).toFixed(2) + "M"); } memmorySizeLogger(); // 1.79M let person = { name: "LeBron", age: 21, tmp: new Array(5 * 1024 * 1024), }; memmorySizeLogger(); // 41.96M let map = new Map(); memmorySizeLogger(); // 41.96M map.set(person, "nice"); memmorySizeLogger(); // 41.96M person = null; memmorySizeLogger(); // 41.96M person鐨勫唴瀛樻病鏈夎鍥炴敹
濡傛灉鎯冲湪杩欑鎯呭喌涓嬫甯窯C锛屾爣璁颁负null鍓嶉渶鍏堟墽琛宮ap.delete(person)WeakMapfunction memmorySizeLogger() { global.gc(); const used = process.memoryUsage().heapUsed; console.log((used / 1024 / 1024).toFixed(2) + "M"); } memmorySizeLogger(); // 1.79M let person = { name: "LeBron", age: 21, tmp: new Array(5 * 1024 * 1024), }; memmorySizeLogger(); // 41.96M let wk = new WeakMap(); memmorySizeLogger(); // 41.96M wk.set(person, "nice"); memmorySizeLogger(); // 41.96M person = null; memmorySizeLogger(); // 1.96M person鐨勫唴瀛樿鍥炴敹
Why WeakMap锛?/h2>
鍦↗S閲岀殑Map API鍏辩敤涓や釜鏁扮粍锛坘ey銆乿alue锛夛紝璁剧疆鐨刱ey銆乿alue閮戒細鍔犲埌杩欎袱涓暟缁勭殑鏈熬锛屽苟瀵筴ey浜х敓寮曠敤銆傚綋浠巑ap鍙栧€兼椂锛岄渶瑕侀亶鍘嗘墍鏈夌殑key锛岀劧鍚庨€氳繃绱㈠紩浠巚alue鏁扮粍涓彇鍑虹浉搴攊ndex鐨勫€笺€?/p>
缂虹偣涓€锛?/p>
璧嬪€笺€佹悳绱㈤兘鏄疧(n)澶嶆潅搴?/p>
缂虹偣浜岋細
浣跨敤Map瀹规槗鍑虹幇鍐呭瓨娉勬紡锛屽洜涓烘暟缁勪竴鐩村紩鐢ㄧ潃姣忎釜key鍜寁alue锛屽鑷存棤娉曟甯窯C銆?/p>
WeakMap瀵筴ey杩涜寮卞紩鐢紝涓嶅奖鍝嶆甯窯C
- key琚獹C鍚庡け鏁?/li>
濡傛灉浣犺寰€瀵硅薄涓婃坊鍔犳暟鎹紝鍙堜笉鎯冲共鎵板瀮鍦惧洖鏀舵満鍒讹紝灏卞彲浠ヤ娇鐢?WeakMap
- 濡傛灉闇€瑕侀亶鍘?/ 杩唬锛屽垯闇€瑕佷娇鐢∕ap
搴旂敤鍦烘櫙
淇濆瓨DOM鑺傜偣鏁版嵁
let domData = new WeakMap(); let dom = document.getElementById("xxx"); const anyDomData = getDomData(dom); domData.set(dom, anyDomData); console.log(domData.get(dom)); dom.parentNode.removeChild(dom); dom = null;
缂撳瓨鐩稿叧鏁版嵁
let cache = new WeakMap(); class HandleCache { get(key) { if (cache.has(key)) { return cache.get(key); } else { return undefined; } } set(key, value) { cache.set(key, value) } delete(key){ cache.delete(key) } }
灏佽绉佹湁灞炴€?/h3>
let privateData = new WeakMap(); class Person{ constructor(name, age){ privateData.set(this,{name, age}); } getData(){ return privateData.get(this); } }
WeakSet
鍓嶇疆鐭ヨ瘑[娣卞叆娴呭嚭]JavaScript GC 鍨冨溇鍥炴敹鏈哄埗浠€涔堟槸WeakSet
WeakSet瀵硅薄鏄竴浜涘璞″€肩殑闆嗗悎锛屽苟涓斿叾涓殑姣忎釜瀵硅薄鍙兘鍑虹幇涓€娆★紝鍦╓eakSet闆嗗悎涓槸鍞竴鐨?/blockquote>鏂规硶
- add(value)
鍦ㄨWeakSet瀵硅薄涓坊鍔犱竴涓柊鐨勫厓绱爒alue
- delete(value)
鍦ㄨWeakSet瀵硅薄涓垹闄alue杩欎釜鍏冪礌鍚庯紝has鏂规硶浼氳繑鍥瀎alse銆?/blockquote>
- has(value)
杩斿洖涓€涓狟oolean鍊硷紝琛ㄧず缁欏畾鐨剉alue鍊兼槸鍚﹀瓨鍦ㄨ繖涓猈eakSet涓?/blockquote>鍜孲et鏈変粈涔堝尯鍒?/h2>
- Set鐨剉alue鍙互鏄换浣曞€硷紝WeakSet鐨勫€煎彧鑳芥槸瀵硅薄
const name = "LeBron"; const age = 21; const person = { name: "LeBron", age: 21, }; const ws = new WeakSet(); const set = new Set(); set.add(name); set.add(age); set.add(person); ws.add(person); ws.add(name); // TypeError: Invalid value used in weak set ws.add(age); // TypeError: Invalid value used in weak set
- Set鏄彲閬嶅巻鐨勶紝WeakSet涓嶅彲閬嶅巻
- Set瀛樺湪涓€涓暟缁勫瓨鏀緑alue鐨勫€硷紝寮曠敤鍘熷璞★紝鏁呭彲閬嶅巻
- WeakSet涓嶅瓨杩欐牱鐨勬暟缁勶紝鏁呬笉鍙亶鍘?/li>
const name = "LeBron"; const age = 21; const person = { name: "LeBron", age: 21, }; const ws = new WeakSet(); const set = new Set(); set.add(name); set.add(age); set.add(person); console.log(set.values()); // { \'LeBron\', 21, { name: \'LeBron\', age: 21 } } ws.add(person); ws.add(name); ws.add(age); console.log(set.values()); // TypeError: ws.values is not a function
- Set褰卞搷GC锛岃€學eakSet涓嶅奖鍝?/li>
- 浠ヤ笅绋嬪簭闇€瑕佹墜鍔℅C 鍚姩鏂规硶锛?code>node --expose-gc xxx
Set瀛樺湪values鏁扮粍锛屽湪鍘焩alue鎸囧悜null鍚庯紝values鏁扮粍浠嶅value鐨勫€煎瓨鍦ㄥ己寮曠敤锛屽奖鍝嶆甯窯Cfunction memmorySizeLogger() { global.gc(); const used = process.memoryUsage().heapUsed; console.log((used / 1024 / 1024).toFixed(2) + "M"); } memmorySizeLogger(); // 1.79M let person = { name: "LeBron", age: 21, tmp: new Array(5 * 1024 * 1024), }; memmorySizeLogger(); // 41.96M const set = new Set(); set.add(person); memmorySizeLogger(); // 41.96M person = null; memmorySizeLogger(); // 41.96M
WeakSet涓嶅瓨鍦ㄨ繖鏍风殑鏁扮粍锛屾晠涓嶅奖鍝嶆甯窯Cfunction memmorySizeLogger() { global.gc(); const used = process.memoryUsage().heapUsed; console.log((used / 1024 / 1024).toFixed(2) + "M"); } memmorySizeLogger(); // 1.79M let person = { name: "LeBron", age: 21, tmp: new Array(5 * 1024 * 1024), }; memmorySizeLogger(); // 41.96M const ws = new WeakSet(); ws.add(person); memmorySizeLogger(); // 41.96M person = null; memmorySizeLogger(); // 1.96M
搴旂敤鍦烘櫙
妫€娴嬪惊鐜紩鐢?/h3>
閫掑綊璋冪敤鑷韩鐨勫嚱鏁伴渶瑕佷竴绉嶉€氳繃璺熻釜鍝簺瀵硅薄宸茶澶勭悊锛屾潵搴斿寰幆鏁版嵁缁撴瀯鐨勬柟娉?/blockquote>// 瀵?浼犲叆鐨剆ubject瀵硅薄 鍐呴儴瀛樺偍鐨勬墍鏈夊唴瀹规墽琛屽洖璋?function execRecursively(fn, subject, _refs = null){ if(!_refs) _refs = new WeakSet(); // 閬垮厤鏃犻檺閫掑綊 if(_refs.has(subject)) return; fn(subject); if("object" === typeof subject){ _refs.add(subject); for(let key in subject) execRecursively(fn, subject[key], _refs); } } const foo = { foo: "Foo", bar: { bar: "Bar" } }; foo.bar.baz = foo; // 寰幆寮曠敤! execRecursively(obj => console.log(obj), foo);
Reflect
Reflect璇戜负鍙嶅皠锛屾槸涓€涓唴缃殑鏂扮殑鍏ㄥ眬瀵硅薄锛屽畠鎻愪緵鎷︽埅JavaScript鎿嶄綔鐨勬柟娉曘€傝繖浜涙柟娉曚笌Proxy handler鐨勬柟娉曠浉鍚屻€俁eflect涓嶆槸涓€涓嚱鏁板璞★紝鏄潤鎬佺殑绫讳技宸ュ叿鍑芥暟锛岀被浼糓ath锛屽洜姝ゅ畠鏄笉鍙瀯閫犵殑Reflect鐨勯潤鎬佹柟娉?/h2>
鍏蜂綋鐢ㄦ硶鍙傝€冿細Reflect MDN鏂囨。
Reflect.apply()
Reflect.construct()
Reflect.defineProperty()
Reflect.deleteProperty()
Reflect.get()
Reflect.getOwnPropertyDescriptor()
Reflect.getPrototypeOf()
Reflect.has()
Reflect.isExtensible()
Reflect.ownKeys()
Reflect.preventExtensions()
Reflect.set()
Reflect.setPrototypeOf()
杩欎簺鏂规硶涓嶱roxy handler鐨勬柟娉曠殑鍛藉悕鐩稿悓锛屽叾涓殑涓€浜涙柟娉曚笌Object鐨勬柟娉曠浉鍚岋紝灏界浜岃€呬箣闂村瓨鍦ㄧ潃鏌愪簺缁嗗井鐨勫樊鍒?/blockquote>
鏈変粈涔堜笉鍚岋紵
Reflect鐨勯潤鎬佹柟娉曡繘琛岀浉搴旀搷浣滀細杩斿洖涓€涓狟oolean鍊?/p>
- 鎿嶄綔鎴愬姛杩斿洖true
- 鎿嶄綔澶辫触杩斿洖false
灏嗗父瑙勭殑鍛戒护寮忔搷浣滆浆鎹㈡垚浜嗗嚱鏁板紡鎿嶄綔锛岀紪绋嬫柟寮忓鍔犱簡鍏冪紪绋嬨€?/p>
- 渚嬪delete銆佽祴鍊笺€佸垽鏂瓑
- 鍛戒护寮忔搷浣滃け璐ヤ竴鑸細鎶ラ敊锛岃€孯eflect涓嶄細锛岃繑鍥炰竴涓狟oolean鍊煎垽鏂槸鍚︽垚鍔熴€?/li>
- 鍐呯疆瀵硅薄涓凡缁忓瓨鍦ㄤ簡涓€浜涘弽灏凙PI锛孯eflect灏嗕粬浠仛鍚堣捣鏉ュ苟杩涜浜嗕紭鍖?/li>
浠€涔堟槸鍏冪紪绋嬶紵
- 鍏冪紪绋嬪嵆瀵圭紪绋嬭瑷€杩涜缂栫▼
- 渚嬪Proxy瀵硅薄鍙互杩涜浠g悊锛屾嫤鎴猤et銆乻et鎿嶄綔
- 鑰屽湪绋嬪簭涓幏鍙栫殑鏄綘缂栫▼鍚庣殑鍊笺€?/li>
Reflect灏辨槸涓€绉嶅弽灏勶紝璋冪敤鐨勬槸澶勭悊杩囧悗鐨勫悇鍐呯疆瀵硅薄涓婄殑鏂规硶
- 鎵€浠ュ悇鍐呯疆瀵硅薄鐨勬柟娉曟敼鍙樺悗锛孯eflect璋冪敤鐨勬柟娉曚篃鏄敼鍙樹簡鐨?/li>
- 绫讳技浜庡皝瑁呬簡涓€灞?/li>
Reflect鐨勪紭鐐?/h2>
- 浼樺寲鍛藉悕绌洪棿
浣犱細鍙戠幇JS鐨勫唴缃弽灏勬柟娉曟暎钀藉湪鍚勫锛孯eflect灏嗕粬浠緢濂藉湴缁勭粐浜嗚捣鏉ャ€?/p>
- 澧炲己浠g爜鐨勫仴澹€?/li>
浣跨敤Reflect杩涜鎿嶄綔涓嶅鏄撴姏鍑哄紓甯搞€佺嚎绋嬮樆濉烇紝浣夸唬鐮佹洿鍋ュ.鍦拌繍琛屻€?/p>
- 涓轰粈涔堜笉鐩存帴鎸傚湪Object涓婏紵
鍙嶅皠鐨勫璞′笉浠呴拡瀵逛簬Object锛岃繕鍙兘閽堝鍑芥暟
- 渚嬪apply锛岃皟鐢∣bject.apply(myFunc)杩樻槸鎸哄鎬殑
鐢ㄤ竴涓崟涓€鐨勫璞′繚瀛樺唴缃柟娉曡兘澶熶繚璇丣avaScript浠g爜鍏朵粬瀵硅薄鐨勭函鍑€鎬?/p>
- 杩欐牱瑕佷紭浜庣洿鎺ュ弽灏勬寕杞藉埌鏋勯€犲嚱鏁版垨鑰呭師褰笂
- 鏇翠紭浜庣洿鎺ヤ娇鐢ㄥ叏灞€鍙橀噺锛岃繖鏍稪S鍏抽敭瀛楀皢瓒婃潵瓒婂銆?/li>
- 鎺橀噾鍘熸枃
- 鎺橀噾锛?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fjuejin.cn%2Fuser%2F3913917127467805" rel="nofollow">鍓嶇LeBron
- 鐭ヤ箮锛?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fwww.zhihu.com%2Fpeople%2Flebronchao" rel="nofollow">鍓嶇LeBron
- 鎸佺画鍒嗕韩鎶€鏈崥鏂囷紝鍏虫敞寰俊鍏紬鍙仿狆煈夝煆?鍓嶇LeBron
以上是关于[绉戞櫘]ES6涓€浜涗笉甯歌鐨勫皬鐭ヨ瘑的主要内容,如果未能解决你的问题,请参考以下文章