当前位置:网站首页>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
边栏推荐
- SIGIR'22「微软」CTR估计:利用上下文信息促进特征表征学习
- Unity general steps for creating a hyper realistic 3D scene
- R语言使用caret包的preProcess函数进行数据预处理:对所有的数据列进行BoxCox变换处理(将非正态分布数据列转换为正态分布数据、不可以处理负数)、设置method参数为BoxCox
- Kubernetes getting started to proficient - install openelb on kubernetes
- 使用 WPAD/PAC 和 JScript在win11中进行远程代码执行
- Cadence OrCAD capture batch change component packaging function introduction graphic tutorial and video demonstration
- CVPR 2022 | QueryDet:使用级联稀疏query加速高分辨率下的小目标检测
- How about Bohai futures. Is it safe to open futures accounts?
- 论文写作 19: 会议论文与期刊论文的区别
- Cadence Orcad Capture CIS更换元器件之Link Database 功能介绍图文教程及视频演示
猜你喜欢
【目标跟踪】基于帧差法结合卡尔曼滤波实现行人姿态识别附matlab代码
MySQL advanced lock - overview of MySQL locks and classification of MySQL locks: global lock (data backup), table level lock (table shared read lock, table exclusive write lock, metadata lock and inte
SQL Server Connectors By Thread Pool | DTSQLServerTP 插件使用说明
[text classification cases] (4) RNN and LSTM film evaluation Tendency Classification, with tensorflow complete code attached
The textarea cursor cannot be controlled by the keyboard due to antd dropdown + modal + textarea
JDBC tool class jdbcfiledateutil uploads files and date format conversion, including the latest, simplest and easiest way to upload single files and multiple files
Shanda Wangan shooting range experimental platform project - personal record (V)
Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (V)
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
DNS cloud school rising posture! Three advanced uses of authoritative DNS
随机推荐
CVPR 2022 | querydet: use cascaded sparse query to accelerate small target detection under high resolution
程序设计语言基础(2)
[target tracking] pedestrian attitude recognition based on frame difference method combined with Kalman filter, with matlab code
SQL Server Connectors By Thread Pool | DTSQLServerTP 插件使用说明
Devops integration - environment variables and building tools of Jenkins service
使用 WPAD/PAC 和 JScript在win11中进行远程代码执行1
Sqoop imports data from Mysql to HDFS using lzop compression format and reports NullPointerException
R语言使用timeROC包计算无竞争风险情况下的生存资料多时间AUC值、使用confint函数计算无竞争风险情况下的生存资料多时间AUC指标的置信区间值
PCL点云处理之直线与平面的交点计算(五十三)
LeetCode动态规划训练营(1~5天)
Confusion about thread blocking after calling the read () method of wrapper flow
Unity 模型整体更改材质
How to do product innovation—— Exploration of product innovation methodology I
中金财富公司怎么样,开户安全吗
DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind
NC basic usage 2
Investigate why close is required after sqlsession is used in mybatties
Leetcode XOR operation
NC basic usage 1
Vericrypt file hard disk encryption tutorial