Maison > Article > interface Web > Un premier aperçu de Map et WeakMap dans ES6
Cet article partage avec vous Map et WeakMap dans ES6. J'espère qu'il pourra aider les amis dans le besoin
let myMap=new Map(); let keyObj={},keyfunc=function(){},keysTring='hello word';
myMap.set(keysTring,'keysTring的关联值') myMap.set(keyfunc,'keyfunc的关联值') myMap.set(keyObj,'keyObj的关联值')
console.log(myMap.size) //3
console.log(myMap.get(keysTring)) //keysTring的关联值 console.log(myMap.get('hello word')) //keysTring的关联值 ---------- console.log(myMap.get(keyfunc)) //keyfunc的关联值 console.log(function(){}===keyfunc) //false console.log(myMap.get(function(){})) //undefined, 因为keyFunc !== function () {} ---------- console.log(myMap.get(keyObj)) //keyObj的关联值 console.log({}===keyObj) //false console.log(myMap.get({})) //undefined 因为keyObj !== {}
myMap.set(NaN, "not a number"); console.log(myMap.get(NaN)) //not a number var otherNaN=Number('foo'); console.log(otherNaN) //NaN console.log(myMap.get(otherNaN)) //not a number 也可以取值
let kvArray = [["key1", "value1"], ["key2", "value2"]]; var myMap1 = new Map(kvArray); //使用映射对象常规的构造函数将一个二维键值对数组对象转换成一个映射关系 console.log(myMap1) //Map { 'key1' => 'value1', 'key2' => 'value2' } console.log(myMap1.get('key1')) //value1 console.log([...myMap1]) //[ [ 'key1', 'value1' ], [ 'key2', 'value2' ] ]La méthode
var myMap2 = new Map(kvArray); myMap2.forEach((value,index)=>{ console.log(value+'---'+index) //value1---key1 value2---key2 })
var myMap3 = new Map(kvArray); for(var a of myMap3){ console.log(a) //[ 'key1', 'value1' ] [ 'key2', 'value2' ] } for(var [key,value] of myMap3){ console.log('key:'+key+', value:'+value) //key:key1, value:value1 key:key2, value:value2 }
let clearMap=new Map(); clearMap.set('hello','word'); console.log(clearMap.size) //1 clearMap.clear(); //清空 console.log(clearMap.size) //0
let delMap=new Map(); delMap.set('hi','word'); delMap.set('hello','word'); console.log(delMap) //Map { 'hi' => 'word', 'hello' => 'word' } delMap.delete('hi'); //执行删除操作 console.log(delMap) //Map { 'hello' => 'word' }
let hasMap=new Map(); hasMap.set('hi','word'); hasMap.set('hi','hello word'); console.log(hasMap) //Map { 'hi' => 'hello word' } 一样的key后面的value会覆盖前面的值 console.log(hasMap.has('hi')); //true console.log(hasMap.has('hello')); //false
let entriesMap=new Map(); entriesMap.set('a','A'); entriesMap.set('b','B'); let entries=entriesMap.entries(); console.log(entries.next()); //{ value: [ 'a', 'A' ], done: false } console.log(entries.next().value); //[ 'b', 'B' ] console.log(entries.next()); //{ value: undefined, done: true }
let valueMap=new Map(); valueMap.set('a','A'); valueMap.set('b','B'); let values=valueMap.values(); console.log(values.next()) //{ value: 'A', done: false } console.log(values.next()) //{ value: 'B', done: false } console.log(values.next()) //{ value: undefined, done: true }
1.delete(key); 根据key删除value 2.get(key);根据key获取value 3.has(key) 根据key检查是不是存在value 4.set(key,value) 根据key设置value let weakMap=new WeakMap(); weakMap.set('hi','word') //异常 因为WeakMap的键只能是个对象 let obj1={}; weakMap.set(obj1,'word') //正常 console.log(weakMap) //WeakMap {} console.log(weakMap.get(obj1)) //word console.log(weakMap.has(obj1)) //true console.log(weakMap.delete(obj1)) //true
Recommandations associées :
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!