Git Product home page Git Product logo

Comments (3)

LeeeeeeM avatar LeeeeeeM commented on August 21, 2024

好了,首先我们要知道,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.

LeeeeeeM avatar LeeeeeeM commented on August 21, 2024

这里看到新的数据结构可以用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.

LeeeeeeM avatar LeeeeeeM commented on August 21, 2024

的确是个问题,所以ES6在引入Map的同时又引入了WeakMap。
ruanyf/es6tutorial#362 后面不说了,自己看评论吧

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.