什么是“全局符号注册表”? [英] What is 'global symbol registry'?

查看:429
本文介绍了什么是“全局符号注册表”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var sym = Symbol(); 

正在字典中创建一个新的属性 sym 窗口),它位于全局范围中,只有 window ['sym']



但是 MDN 说:


上面的语法使用 Symbol() code> function 不会创建整个代码库中可用的全局符号。要在文件和全局范围环境之间创建可用的符号,请使用方法 Symbol.for() Symbol.keyFor()从全局符号注册表设置和检索符号。


sym 已经在浏览器的全局范围内,具有上述声明语法。



什么是全局符号注册表?



每个html文档与窗口对象绑定。



所以
在浏览器世界中,跨全局范围(窗口)中的文件/领域符号可用性范围如何不同对象)?

解决方案


  var sym = Symbol ; 

正在字典中创建一个新的属性 sym 窗口),它在全局范围内,其中可以以 window ['sym'] / p>

嗯,不。它确实创建一个符号,并将其分配给名为 sym 局部变量。只有当您在全局范围(通常不会为模块化)执行此代码时,它将在您的领域(js环境)的全局对象上创建一个属性。请注意,这个全局对象不一定总是窗口像网页一样,这取决于你的环境。


什么是全局符号注册表?


这是一个注册表(认为:字典),您可以通过字符串键。而在这种情况下,全局在全球范围内意味着甚至比全局范围更加全局化,全局符号注册表覆盖了引擎的所有领域。在浏览器中,网页,iframe和web worker都将拥有自己的全局对象,但可以通过这个全局注册表共享符号。



这个共享正是这个目的。如果你还是把

  var sym1 = Symbol(shared); 

var sym2 = Symbol(shared);

在两个地方,然后 sym1!== sym2 。如果您有一个共享对象,使用符号 s 作为属性键将创建两个不同的属性。然而,如果你执行

  var sym1 = Symbol.for(shared); 

var sym2 = Symbol.for(shared);

然后 sym1 === sym2 你使用它总是会得到相同的财产。



另见 跨越符号的领域在2ality 符号,为什么他们真棒 更多的例子,包括类似全球的知名符号。 >

var sym = Symbol();

is creating a new property sym in dictionary(window) , which is in global scope, nothing but window['sym'].

But MDN says:

The above syntax using the Symbol() function will not create a global symbol that is available in your whole codebase. To create symbols available across files and in a global scope-like environment, use the methods Symbol.for() and Symbol.keyFor() to set and retrieve symbols from the global symbol registry.

sym is already in global scope in a browser, with above declaration syntax.

What is global symbol registry?

Each html document is tied with window object.

So, In a browser world, How does this scope of symbol availability across files/realms different from global scope(window object)?

解决方案

var sym = Symbol();

is creating a new property sym in dictionary(window), which is in global scope, where value can be accessed as window['sym'].

Well, no. It does create a symbol and assigns it to a local variable named sym. Only if you are executing this code in the global scope (which you usually wouldn't, for modularity) it does create a property on the global object of your realm (js environment). Notice that this global object is not always window like in web pages, it depends on your environment.

What is global symbol registry?

It's a registry (think: dictionary) for symbols that you can access via a string key. And "global" does in this case mean even more global than a global scope, the global symbol registry does span all realms of your engine. In a browser, the web page, an iframe, and web worker would all have their own realm with own global objects, but they could share symbols via this global registry.

And this sharing is exactly the purpose. If you'd otherwise put

var sym1 = Symbol("shared");

var sym2 = Symbol("shared");

in two places, then sym1 !== sym2. If you've got a shared object, using the symbols as property keys would create two different properties. If however you do

var sym1 = Symbol.for("shared");

var sym2 = Symbol.for("shared");

then sym1 === sym2 and when you use it you'll always get the same property.

See also Crossing realms with symbols on 2ality and Symbols and why they're awesome for more examples, including the well-known symbols which are similarly global.

这篇关于什么是“全局符号注册表”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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