当前位置:网站首页>Why does ES6 need to introduce map when JS already has object type

Why does ES6 need to introduce map when JS already has object type

2022-04-23 20:16:00 _ Carpediem

object

1、 All object property names must be of type string , It must be ensured that Each key name is a string type and is unique in the object

When the key name of the attribute is Number Type :

var map=Object.create(null)
map[5]='hello'
conslole.log(map['5'])  //'hello'
conslole.log(map[5])   //'hello'

In this example, the object (map) Properties of (5) Assign as string ’hello’, The key name of this attribute is numeric 5, It will be Automatically ( mandatory ) conversion Is a string type , therefore map[5]===map[‘5’], They actually refer to the same attribute .

  • When the key name of the attribute is Object Type :
var map=Object.create(null)
var key1={
    },key2={
    }
map[key1]='hello'
console.log(map[key2]) //'hello'
console.log(map[key2]) //'hello'

In this case key1 and key2 Will be converted to the string corresponding to the object ’[Object Object]’, therefore map[key1]===map[]key2], It refers to the same attribute .

Map

1. ECMAScript6 Medium Map A type is one that stores many An ordered list of key value pairs , The key name and its corresponding value Support all data types .

2. Map Collection support methods :

  • has(key)—— return Boolean, Detect that the specified key is in Map Whether the collection already exists

  • delete(key)—— from Map Collection Delete the specified key value pair

  • clear()—— Delete Map Collection All key value pairs

  • forEach()—— Traverse and manipulate each value in the collection

    let map=new Map([['name','dog'],['age',3]])
    map.forEach(function(value,key,ownerMap){
          //forEach The callback function of the method accepts 3 Parameters ,( value , Key name ( In the array, the corresponding index value is numeric ),Map The assembly itself )
        console.log(key+' '+value)
        console.log(map===ownerMap)
    })
    //output:
    //name dog
    //true
    //age 3
    //true
    
  • set()—— Add any type of value to the collection

  • get()—— Retrieve all values in the collection

  • size—— adopt size Property can check the number of values contained in the collection

3. Map Set initialization

let map=new Map()
map.set('name','dog')
map.set('age',3)
// Empathy 
// let map=new Map([['name','dog'],['age',3]])

console.log(map.size)  //2
console.log(map.has('name'))//true
console.log(map.get('name'))//'dog'

map.delete('age')
console.log(map.has('age'))//false
console.log(map.get('age'))//undefined
console.log(map.size)  //1

map.clear()
console.log(map.has('name'))//false
console.log(map.size)//0

WeakMap

1. This set is a special kind of Map aggregate , Only key names of object types are supported .

2. Stored in the collection Keys are weak references to objects

3. Key name enumeration is not supported : That is not supported forEach() and clear() Method

4. Use scenarios :

  • Add additional information for those objects whose actual use is separated from lifecycle management ;

  • Storage DOM Elements ;

  • Store private properties of object instances

    let Person=(function(){
          
    	let privateData=new WeakMap() // Store private properties 
    	function Person(name){
              //Person Instance of object (this) It can be used directly as the key of the collection 
    		privateData.set(this,{
          name:name})// call Person Constructor , New entries will be added to WeakMap Collection , The key of the entry is this, Value is the private information contained in the object ( Which contains name Object of property )
    	}
    	Person.prototype.getName=function(){
          
    		return privateData.get(this).name// Access to private information 
    	}
    	return Person // As long as the object instance is destroyed , Relevant information will also be destroyed , So as to ensure the privacy of information 
    }())   
    

版权声明
本文为[_ Carpediem]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210553546522.html