Symbol.for(string) in ECMAScript 6

前端 未结 3 1602
你的背包
你的背包 2020-12-03 18:24

It took me a while but I finally figured out what the purpose of symbols in ECMAScript 6 is: avoiding name collision when attaching properties to shared objects - HTML eleme

3条回答
  •  旧时难觅i
    2020-12-03 18:37

    I invented the most useful feature of Symbol.for() call. If there is using symbols in your code sometimes it is difficult to use conditional breakpoints while debugging. For example, you need to catch if the variable equals the value which is of symbol type and this value binded in the different module. The first difficult way is to use this value as a constant and export it from that module. In this case, the condition of the breakpoint will look:

    catchedVariable === exportedSymbolConst
    

    But the easiest way is to temporarily change the code inside the module adding .for to Symbol. Then you can write the condition:

    catchedVariable === Symbol.for('string_key')
    

    After the successful debugging you will be changing the code back just removing .for part.

提交回复
热议问题