The original motivation for introducing symbols to Javascript was to enable private properties.(将符号引入Javascript的最初动机是启用私有属性。)
Unfortunately, they ended up being severely downgraded.(不幸的是,它们最终被严重降级。) They are no longer private, since you can find them via reflection, for example, using Object.getOwnPropertySymbols
or proxies.(它们不再是私有的,因为您可以通过反射找到它们,例如,使用Object.getOwnPropertySymbols
或代理。)
They are now known as unique symbols and their only intended use is to avoid name clashes between properties.(它们现在被称为唯一符号,它们的唯一用途是避免属性之间的名称冲突。) For example, ECMAScript itself can now introduce extension hooks via certain methods that you can put on objects (eg to define their iteration protocol) without risking them to clash with user names.(例如,ECMAScript本身现在可以通过某些方法引入扩展挂钩,您可以将这些挂钩应用于对象(例如,定义其迭代协议),而不必冒与用户名冲突的风险。)
Whether that is strong enough a motivation to add symbols to the language is debatable.(是否足够强大,是否有动机在语言中添加符号尚有争议。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…