Recover property key / value

I play with ECMAScript 6 symbols and cards in Node.JS v0.11.4 with the --harmony flag. Consider the following.

 var a = Map(); a.set(Symbol(), 'Noise'); // Prints "1" console.log(a.size); 

Is it possible to get a value of 'Noise' if the property is identified using a “anonymous” symbol key, which is guaranteed to be unique?

+6
source share
1 answer

This cannot be done in node.js because the current version of v8 did not implement iteration, as indicated in this error message .

We can confirm this by looking at the source code for v8 collection.js :

 InstallFunctions($Map.prototype, DONT_ENUM, $Array( "get", MapGet, "set", MapSet, "has", MapHas, "delete", MapDelete, "clear", MapClear )); 

But, as you can see in the ECMAScript 6 wiki , there should also be items() , keys() and values() . v8 probably did not use these methods before, because it did not support generators. But now he has been doing this May. It should just be a matter of time until it is implemented.

If you need to have this functionality now, you can use map-set-for-each , which populates forEach . You will need to modify it to add case 'symbol': after case 'object':

 a.forEach(function(value, key) { if (value === 'Noise') { console.log('Give mak the bounty'); } }); 

When v8 implements the Map iteration, you can find Noise as follows:

 for (let [key, value] of a) { if (value === 'Noise') { console.log('Upvotes for future'); } } 
+5
source

Source: https://habr.com/ru/post/948981/


All Articles