the murmurous sea

JS ES6: symbol 본문

#dev/개념정리

JS ES6: symbol

lunacer 2020. 5. 15. 12:21

After ECMAScript, JavaScript used to have 6 data types.

Primitive data type - string 
- number
- bigint 
- boolean 
- undefined
Object type Object(object, array, function)

After ES6(ECMAScript2015), symbol is added as a 7th data type.

about null

더보기

There also is null, which is seemingly primitive, but indeed is a special case for every Object: and any structured type is derived from null by the Prototype Chain.
(https://developer.mozilla.org/en-US/docs/Glossary/Primitive)

 

[Symbol type]

1. Primitive data type
2. is the set of all non-String values that may be used as the key of an Object property
3. Each possible Symbol value is unique and immutable.
4. A value having the data type Symbol can be referred to as a "Symbol value". 

   : is created by invoking the function Symbol
   : dynamically produces an anonymous, unique value.

5. can have an optional description, but for debugging purposes only.

   : Each Symbol value immutably holds an associated value called [[Description]] that is either undefined or a String value.

6. Symbols don't "Auto-Convert" to strings

   : Most values in JavaScript support implicit conversion to a string. Symbols are special. They don’t auto-convert.

 

※ Ruby's (or another language) that also has a feature called "symbols", don’t be misguided. JavaScript symbols are different.
    Symbol type is a new feature in ECMAScript 2015. There is no ECMAScript 5 equivalent for Symbol.
    In some programming languages, the symbol data type is referred to as an "atom."

 

To show a symbol,

: There’s a "language guard" against messing up(No auto-convert),
: because strings and symbols are fundamentally different, and should not occasionally convert one into another.

1. symbol.toString()
2. symbol.description

 

Symbol() function

- returns a value of type symbol

   : Every symbol value returned from Symbol() is unique.  
   : A Symbol value represents a unique identifier. 
- has static properties that expose several members of built-in objects
- has static methods that expose the global symbol registry
- resembles a built-in object class
- is incomplete as a constructor because it does not support the syntax "new Symbol()".  


Use-cases

1. A symbol value may be used as an identifier for object properties; this is the data type's primary purpose, 
2. Enabling opaque data types
3. Serving as an implementation-supported unique identifier in general.  

 

Well-known symbols

- are built-in Symbol values that are explicitly referenced by algorithms of this specification.

- 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 realms

 

Global symbol registry

- Holding all available symbols.
- Global symbol registry's content is not available to JavaScript's run-time infrastructure, except through these reflective methods
   Symbol.for() and Symbol.keyFor()
   : The methods that access the registry: 

   : These mediate between the global symbol table (or "registry") and the run-time environment.

Symbol.for(tokenString)
  : returns a symbol value from the registry.

Symbol.keyFor(symbolValue)
  : returns a token string from the registry.

Each is the other's inverse, so the following is true:
Symbol.keyFor(Symbol.for("tokenString")) === "tokenString" // true

 

 

Object.getOwnPropertySymbols 

Symbol.iterator


www.developer.mozilla.org/en-US/docs/Glossary/Symbol

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol

www.tc39.es/ecma262/#sec-ecmascript-language-types-symbol-type

https://medium.com/@hyunwoojo/javascript-symbol-%EC%97%90-%EB%8C%80%ED%95%B4%EC%84%9C-6aa5903fb6f1

v=v03IdCKwUAAhttps://medium.com/@pks2974/javascript%EC%99%80-%EC%8B%AC%EB%B3%BC-symbol-

Couldn't understand this video lecture: www.youtube.com/watch?

'#dev > 개념정리' 카테고리의 다른 글

JS: Promise.prototype.then() - incompleted  (0) 2020.05.21
JS ES6: Promise  (0) 2020.05.21
생성자 함수 (empty)  (0) 2020.05.14
JS: split() vs. join()  (0) 2020.05.14
JS: The Arguments Object  (0) 2020.05.14
Comments