在ES6 JavaScript中,@@(“at at”)是什么意思? [英] What does @@ ("at at") mean in ES6 JavaScript?

查看:427
本文介绍了在ES6 JavaScript中,@@(“at at”)是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到 @@ rel =noreferrer>几个 pages < a>关于新的ES6功能,但是我不知道究竟是什么意思(无论是语法还是某种文档约定)。这很难google。有人可以解释一下吗?

@@ 已知符号。 (请注意,它在JS中实际上不是有效的语法。)根据ES6 / ES2015 1 规范


着名的符号是内置符号值由本规范的算法明确引用。它们通常用作其值用作规范算法的扩展点的属性的键。除非另有说明,所有代码领域(8.2)共享众所周知的符号值。


代码域指的是不同的JavaScript环境。例如,根文档的代码领域将与在< iframe> 中运行的JavaScript的代码领域不同。



一个对象来自何方的代码领域的例子是试图使用 instanceof 来确定一个对象是否是一个数组(提示:它不会如果从另一个框架工作)。为了避免这些类型的问题从符号弹出,它们是共享的,所以引用(说) @@ toString 将会工作,无论对象来自哪里。其中一些直接通过 Symbol 构造函数公开,例如 @@ toPrimitive 被公开为 Symbol.toPrimitive 。这可以用于覆盖尝试将对象转换为原始值时生成的值,例如:

  let a = {[Symbol.toPrimitive]:()=> 1}; 
console.log(+ a); // 1
console.log(a.valueOf()); //(同一个对象)
console.log(a.toString()); //[object Object]

通常,符号用于在对象上提供唯一的属性不能与随机属性名称相冲突,例如:

  let a = Symbol(); 
let foo = {[a]:1};
foo [a]; // 1

除了从某个地方获取符号之外,没有办法访问该值(虽然你可以通过调用 Object.getOwnPropertySymbols 获取对象的所有符号,因此不能用于实现私有属性或方法。



1:有关某些讨论,请参阅此es讨论主题不同的名字。


I've noticed @@ used in a few pages about new ES6 features, but I don't know what exactly it means (whether it's actually syntax or just some kind of documentation convention). And it's hard to google. Can someone explain it?

解决方案

@@ describes what's called a well-known symbol. (Note that it isn't actually valid syntax in JS.) According to the ES6/ES20151 specification:

Well-known symbols are built-in Symbol values that are explicitly referenced by algorithms of this specification. They are typically used as the keys of properties whose values serve as extension points of a specification algorithm. Unless otherwise specified, well-known symbols values are shared by all Code Realms (8.2).

Code Realms refer to different instances of a JavaScript environment. For example, the Code Realm of the root document would be different to that of JavaScript running in an <iframe>.

An example of where it matter what code realm an object comes from is when trying to use instanceof to determine whether an object is an array (hint: it won't work if it's from another frame). To avoid these kinds of issues from popping up with symbols, they are shared so that references to (say) @@toString will work no matter where the object came from.

Some of these are exposed directly through the Symbol constructor, for example, @@toPrimitive is exposed as Symbol.toPrimitive. That can be used to override the value produced when attempting to convert an object to a primitive value, for example:

let a = { [Symbol.toPrimitive]: () => 1 };
console.log(+a); // 1
console.log(a.valueOf()); // (the same object)
console.log(a.toString()); // "[object Object]"

In general, symbols are used to provide unique properties on an object which cannot collide with a random property name, for example:

let a = Symbol();
let foo = { [a]: 1 };
foo[a]; // 1

There is no way to access the value except by getting the symbol from somewhere (though you can get all symbols for an object by calling Object.getOwnPropertySymbols, so they cannot be used to implement private properties or methods).

1: See this es-discuss topic for some discussion about the different names.

这篇关于在ES6 JavaScript中,@@(“at at”)是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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