Symbol数据类型的应用 [英] Application of the Symbol datatype

查看:70
本文介绍了Symbol数据类型的应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出自己,但没有任何成功。我想知道是否有一个有效的不可变数据类型的应用程序,符号

I tried to figure out myself but without any success. I am wondering if there is a valid application for the immutable datatype, Symbol.

有谁知道吗?

推荐答案

符号的优点是它们是唯一的,不会与另一个实例发生冲突(假设你没有使用全局符号)注册表)。这对于模块化,对象代理和库对象的向前兼容扩展非常重要。

The advantage of Symbols is that they are unique and cannot collide with another instance (assuming you're not using the global symbol registry). This is important for modularization, object proxies and forward-compatible extension of library objects.

请考虑以下情形:您为浏览器编码。你可以修改原型,比如HTMLElement,为你的库添加一个新的属性。
现在whatwg / w3c出现并为标准添加一个具有相同名称的属性。突然,您的库与Web标准冲突。

Consider the following scenario: You code for a browser. You modify the prototype of, let's say HTMLElement, adding a new property for your library. Now the whatwg/w3c come along and add an identically-named property to the standard. Suddenly your library is in conflict with the web standards.

当您继承库方法时,会发生类似的事情。导入新版本的库,它突然可能有新的方法与你的碰撞。

Similar things happen when you subclass library methods. Import a new version of the library and it suddenly might have new methods that collide with yours.

这不会发生在符号上,因为你首先实例化一个新的,自定义的,本地符号不能与任何其他人的符号冲突。然后您可以将其用作不会与任何第三方代码冲突的密钥。

This doesn't happen with symbols because you first instantiate a new, custom, local symbol that cannot collide with anyone else's symbols. Then you can use that as key that won't collide with any 3rd party code.

使用模块,您只需将模块的符号导出为常量,使其可供其他人访问模块,但只能通过适当的,基于模块的命名空间,再次避免冲突。

With modules you can simply export the module's symbols as constants, making them accessible to other modules, but only through proper, module-based namespacing, again avoiding collisions.

类也可以使用它们来表示对某些接口的遵从性(因为javascript没有多重继承或接口),如 Symbol.iterator 不会冒名称冲突或不得不在自己的方法名称上妥协。

Classes can also use them to signal compliance with certain interfaces (since javascript has no multiple inheritance or interfaces), like Symbol.iterator does without risking name collisions or having to compromise on its own method names.

此外,它们还可以用作函数参数,以避免魔术字符串常量,这在特别危险时会特别危险它们与同一参数中的常规字符串处理混合在一起。更不用说未声明的符号(通常作为常量导入)可能导致更多可预测的错误,而不是传入的未知字符串,只是通过方法内的特殊外壳掉落。

Additionally they can also be used as function arguments to avoid magic string constants which can be especially hazardous when they are mixed with regular string processing in the same argument. Not to mention that an undeclared symbol (normally imported as a constant) may lead to more predictable errors than an unknown string being passed in and simply falling through the special-casing inside the method.

它们也可能提供性能优势,因为与符号相比,基本上是O(1)指针比较而不是字符串比较。至少在编译器无法推断字符串常量相同的情况下。

They might also offer a performance advantage since comparing to a symbol basically is a O(1) pointer comparison instead of a string comparison. At least in cases where the compiler can't infer that string constants are identical.

这篇关于Symbol数据类型的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆