Skip to content

Commit c7e7e51

Browse files
committed
sybmols initiated.
1 parent 271011b commit c7e7e51

File tree

1 file changed

+37
-0
lines changed
  • 02-Part II - ES2015 features/13-Symbols

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Symbols
2+
3+
* [Source:](https://developer.mozilla.org/en-US/docs/Glossary/Symbol)
4+
5+
> The data type `symbol` is a `primitive data type` having the quality that values of this type can be used to `make object properties` that are anonymous. This data type is `used as the key` for an object property when the property is intended to be `private`, for the internal use of a class or an object type.
6+
7+
* A value having the data type "symbol" can be referred to as a `"symbol value"`.
8+
9+
> a symbol value is created by invoking the `function Symbol()`, which `dynamically produces an anonymous, unique value`.
10+
11+
* The only sensible usage is to store the symbol and then use the stored value to create an object property.
12+
13+
```js
14+
var myPrivateMethod = Symbol(); // stores the symbol in a 'var'
15+
this[myPrivateMethod] = function() {...};
16+
```
17+
18+
> When a symbol value is `used as the identifier in a property` assignment, the property (like the symbol) is `anonymous`; and also is `non-enumerable`. Because the property is non-enumerable, it will not show up as a member in the loop construct "for( ... in ...)", and because the property is anonymous, it `will not show up` in the result array of "Object.getOwnPropertyNames()".
19+
20+
* Symbol values provide a way by which `custom classes can create private members`, and maintain a symbol registry that pertains just to that class.
21+
22+
> there is no ECMAScript 5 equivalent for symbol.
23+
24+
```js
25+
Symbol("foo") !== Symbol("foo")
26+
const foo = Symbol()
27+
const bar = Symbol()
28+
typeof foo === "symbol"
29+
typeof bar === "symbol"
30+
let obj = {}
31+
obj[foo] = "foo"
32+
obj[bar] = "bar"
33+
JSON.stringify(obj) // {}
34+
Object.keys(obj) // []
35+
Object.getOwnPropertyNames(obj) // []
36+
Object.getOwnPropertySymbols(obj) // [ Symbol(), Symbol() ]
37+
```

0 commit comments

Comments
 (0)