Map/Object in javascript does not support Element as key -
my question how use element key in vanilla javascript. before es6, object , other map-liked structures use tostring() identify keys -- means impossible distinguish elements.
https://code.google.com/p/closure-library/issues/detail?id=541 google closure library, vanilla javascript has same issue.
es6 map resolves this, great! usual, how old browsers not support es6...
the usual solution problem in es5 generate unique string key (a monotomically increasing number turned string works fine), assign dom object custom property , use string key in object/map.
this gives unique string, can use map key , if given dom object again , asked in map, can check special key and, if found, use check map. if dom object not have unique key, must not have been in 1 of these maps yet.
here's javascript object implements automatically (objectset): https://github.com/jfriend00/javascript-set/blob/master/objectset.js related code in answer: mimicking sets in javascript?.
or, here's partial polyfill es6 set object implements string key generation automatically when using objects in set.
Comments
Post a Comment