Created
May 9, 2019 03:49
-
-
Save ialex32x/8d9ffb7aa7fe0ecf5d0b5c417d3f5daa to your computer and use it in GitHub Desktop.
nonstring object as key in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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