Comments (3)
好了,首先我们要知道,js原始对象的key值只能是字符串string类型。
var b = {
name: 'mi'
}
var a = {}
a[b] = 123
// {[object Object]: 123}
不管你的对象多复杂,始终要调用toString方法转化为字符串。所以想用对象作为key值在以前是没法实现的。直到后来引入了Map和Set。
var c = Symbol.for('mi')
var user = {
name: 'mi',
age: 30
}
var m = new Map()
m.set('name', 'mi')
m.set(c, 123)
m.set(user, 'mi')
// Map(3) {"name" => "mi", Symbol(mi) => 123, {…} => "mi"}
from blog.
这里看到新的数据结构可以用Object作为key。
但是,问题又来了,假设我作为key的对象被有意无意删除,那么我的value是无论如何都无法再获取的。
var c = Symbol.for('mi')
var user = {
name: 'mi',
age: 30
}
var m = new Map()
m.set('name', 'mi')
m.set(c, 123)
m.set(user, 'mi')
user = null
m.get(user)
// undefined Map(3) {"name" => "mi", Symbol(mi) => 123, {…} => "mi"}
懵逼!明明获取不到user,然后Map实例里面依然保存对象。假设通过构造函数大量构造地对象,之后又做了批量操作,内存有大量的垃圾对象被引用而无法被GC掉,怎么办??
from blog.
的确是个问题,所以ES6在引入Map的同时又引入了WeakMap。
ruanyf/es6tutorial#362 后面不说了,自己看评论吧
from blog.
Related Issues (20)
- 毁三观js
- Vue slot的使用。 HOT 2
- 最后的整合,Vue与快应用发送指令策略。
- Vue v-model 指令的移植
- vue的一些使用规范,自我体会版。
- Vuex 使用心得
- go context包的使用心得
- typescript的各种import方式 HOT 2
- react HOC ref的使用。 HOT 2
- 快应用平台引入Vue框架
- reactComponent和reactElement的区别 HOT 1
- 实现promise
- js回调的一些使用 HOT 7
- 疯狂搞递归。 HOT 2
- leetcode 42
- addEventListener的option选项 HOT 1
- 设计一个并发为2的调度器 HOT 2
- 精读immer系列 HOT 3
- 设计模式记录---模板方法 HOT 1
- 设计模式记录---单例模式 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.