当前位置:网站首页>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
边栏推荐
- 使用 WPAD/PAC 和 JScript在win11中进行远程代码执行3
- 网络通信基础(局域网、广域网、IP地址、端口号、协议、封装、分用)
- 中金财富公司怎么样,开户安全吗
- Five minutes to show you what JWT is
- [problem solving] 'ASCII' codec can't encode characters in position XX XX: ordinal not in range (128)
- Comment créer un pass BEP - 20 sur la chaîne BNB
- Kubernetes entry to mastery - bare metal loadbalance 80 443 port exposure precautions
- 本地调用feign接口报404
- Change the material of unity model as a whole
- The R language uses the timeroc package to calculate the multi time AUC value of survival data without competitive risk, and uses the confint function to calculate the confidence interval value of mul
猜你喜欢
WordPress插件:WP-China-Yes解决国内访问官网慢的方法
SQL Server Connectors By Thread Pool | DTSQLServerTP 插件使用说明
WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
Fundamentals of programming language (2)
STM32基础知识
波场DAO新物种下场,USDD如何破局稳定币市场?
CVPR 2022 | querydet: use cascaded sparse query to accelerate small target detection under high resolution
【目标跟踪】基于帧差法结合卡尔曼滤波实现行人姿态识别附matlab代码
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
随机推荐
nc基础用法1
【2022】将3D目标检测看作序列预测-Point2Seq: Detecting 3D Objects as Sequences
Cadence Orcad Capture CIS更换元器件之Link Database 功能介绍图文教程及视频演示
WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report
使用 WPAD/PAC 和 JScript在win11中进行远程代码执行3
Introduction to link database function of cadence OrCAD capture CIS replacement components, graphic tutorial and video demonstration
Redis installation (centos7 command line installation)
Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (VII)
【问题解决】‘ascii‘ codec can‘t encode characters in position xx-xx: ordinal not in range(128)
Design of warehouse management database system
[2022] regard 3D target detection as sequence prediction - point2seq: detecting 3D objects as sequences
SQL Server Connectors By Thread Pool | DTSQLServerTP 插件使用说明
DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind
aqs的学习
【文本分类案例】(4) RNN、LSTM 电影评价倾向分类,附TensorFlow完整代码
R language uses econocrats package to create microeconomic or macroeconomic map, visualize indifference function indifference curve, customize calculation intersection, and customize the parameters of
STM32基础知识
Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (6)
Cadence OrCAD capture batch change component packaging function introduction graphic tutorial and video demonstration