JavaScript Character Type: (non-string object keys)

What is javascript of type "Symbol" as specified in this ECMAScript 6 project ?

To quote the specification:

A character type is a set of all non-String values ​​that can be used as the key of an Object property.

Each possible value of a symbol is unique and unchanged.

Character values ​​have one observable attribute called [[Private]], whose immutable value is either true or false. A private symbol is a Symbol whose attribute [[Private]] is true.

I thought that only the lines are the object key, and I'm not alone. To vote this accepted an SO answer :

... object keys are always strings ...

Could you explain what type of symbol is and demonstrate its use. I am trying to understand the specification.

Thanks!

+7
source share
3 answers

I thought the keys of the objects were just strings

You are right, but this is true only for EcmaScript 5. ES 6 / harmony is a project for something new!

I am trying to understand the specification

This is just a rough draft. How the characters are used and how they can be created by arbitrary scripts, it seems, has not yet been installed (see the versions for changes).

If you scroll down to the very end of this document (even below Appendix F), for example, you will see section 8.4.4: Exotic Symbol objects that were moved there. It indicates

Exotic Symbol objects provide alternative definitions for all major internal methods.

You can see them, for example, in the section section 8.1.7.4 "Well-known symbols and entrails" . For the proposed use (and still existing issues / open questions) of the Symbol constructors, see these strawman pages or this wiki site .

+6
source

Symbol is a new addition to the language proposed as part of ECMAScript 6 :

Ongoing work on ECMAScript β„’

Work on future releases of ECMAScript β„’ continues as part of the previously announced ECMAScript β„’ Harmony project. More information on ongoing work on ECMAScript Harmony is described in this wiki. The sixth edition of the standard is under development, with a target date of December 2013 for completion.

0
source

We use symbols to make object properties or methods private. Therefore, we hide the details and show only the most necessary. This is called abstraction.

How to implement this: Let's create a simple class with the radius property.

 class Circle { constructor(radius) { this.radius = radius; } } 

A character is essentially a unique identifier. Each time we call this function, we get a unique identifier. However, this is not a constructor function.

 Symbol()===Symbol() //will be false 

Implementation:

 const _radius=Symbol() class Circle { constructor(radius) { this[_radius] = radius; //since property name starts with _, we use bracket notation } } 

Now check this out. Create an instance of Circle:

 const c=new Circle; console.log(Object.getOwnPropertyNames(c))// you will see a number on the console. 
0
source

Source: https://habr.com/ru/post/949885/


All Articles