Skip to content

Instantly share code, notes, and snippets.

@ialex32x
Created May 9, 2019 03:49
Show Gist options
  • Save ialex32x/8d9ffb7aa7fe0ecf5d0b5c417d3f5daa to your computer and use it in GitHub Desktop.
Save ialex32x/8d9ffb7aa7fe0ecf5d0b5c417d3f5daa to your computer and use it in GitHub Desktop.
nonstring object as key in javascript
/*
js 总是将key转化为string, 因此不能用像lua那样直接用function等对象作为key
可以利用 Object.defineProperty 来安插 key 来满足近似的需求
*/
var map = {}
var id = 0
function f1() {}
function f2() {}
function addvalue(k, v) {
id++
Object.defineProperty(k, "__id", id)
map[id] = v
}
function getvalue(k) {
return map[k.__id]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment