Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- express-handlebars
- JS
- close together
- node
- just-one-small-sip
- select by attribute
- Prototype
- css
- 디자인패턴
- Express
- Sass
- css 오버레이
- flex-shrink
- flex-basis
- regExp
- flexbox
- Engoo
- css grid
- img 확대
- 무료 백엔드 배포
- css variables
- ajax
- improve-iq-by-up-to-10%!
- es6
- Object.create
- Node.js
- flex-grow
- module wrapper function
- shit-christmas
- 정규표현식
Archives
- Today
- Total
ES6 Symbol 본문
Symbol은 ES6에서 추가된 primitive data type 입니다.
각각의 Symbol들은 같을 수 없습니다.
Symbol은 객체의 프로퍼티 식별자(프로퍼티의 key)로 이용하기 위해 만들어졌다고 합니다.
for .. in 문을 통해 열거할 수 없고, JSON.stringify 메서드를 이용해 JSON으로 변경할 수 없어 해당 프로퍼티를 감추는 용도로 사용합니다. 해당 프로퍼티의 값에 접근하려면 값의 할당에 쓰였던 Symbol이 있어야하기 때문이죠. ( ex. obj[KEY1] )
아래의 코드를 참조하여 특성에 대해 알아보아요.
// Create a symbol
const sym1 = Symbol();
const sym2 = Symbol("sym2");
console.log(sym1);
console.log(sym2);
// primitive type
console.log(typeof sym2);
// 각 Symbol 들은 같을 수가 없다.
console.log(Symbol() === Symbol()); // false
console.log(Symbol("123") === Symbol("123"));
console.log(`Hello ${String(sym1)}`);
// Unique Object Keys
const KEY1 = Symbol();
const KEY2 = Symbol("sym2");
const KEY3 = "Not Symbol";
// 객체의 프로퍼티 키를 key1으로 유니크하게 갖고 싶을 경우
const obj = {};
obj[KEY1] = "Prop1"; // inenumerable, cant be JSON
obj[KEY2] = "Prop2"; // inenumerable, cant be JSON
obj.key3 = "Prop3"; // enumerable, can be JSON
obj.key4 = "Prop4"; // enumerable, can be JSON
obj["key5"] = "Prop5"; // enumerable, can be JSON
obj[KEY3] = "Prop6"; // enumerable, can be JSON
// console.log(obj[KEY1]);
// console.log(obj[KEY2]);
// Symbols are not enumerable in for... in
for(let i in obj) {
// 심볼을 프로퍼티의 키로 설정할 경우 in 반복문을 이용해 표현할 수 없다. (열거할 수 없다. == not enumerable)
console.log(`${i}: ${obj[i]}`);
}
// Symbols are ignored by JSON.stringify
console.log(JSON.stringify(obj));
'2021 프론트 엔드 로드맵 따라가기 > JS' 카테고리의 다른 글
ES6 Map, Map 컬렉션 in JS (0) | 2021.06.16 |
---|---|
ES6 Destructuring and Rest Parameter, 구조분해할당 (0) | 2021.06.16 |
ES6 Iterator & Generator (0) | 2021.06.15 |
Character Class & Assertions in Regular Expression (0) | 2021.06.14 |
Character Sets, Quantifier and Grouping in Regular Expression, 정규표현식에서의 문자열 집합, 수량자 그리고 그룹화 (0) | 2021.06.13 |
Comments