YeonjuOHYE / javascript

0 stars 0 forks source link

유.돈.노#3) 네이티브 #3

Open YeonjuOHYE opened 5 years ago

YeonjuOHYE commented 5 years ago

네이티브

내장 함수를 말한다

var a = new String("a");
typeof a; // Object
a instanceof String; // true 

3.1 내부 [[Class]]

typeof 의 object에는 [[Class]]라는 내부프로퍼티가 추가로 붙는다. 직접 확인은 불가능하고, Object.prototype.toString()으로 확인 가능 이는 내장 네이티브 생성자를 가르킨다.(그렇지 않을 떄도 있다 -> null,undefined)

3.1 내부 [[Class]]

래퍼 박싱을 통해 primitive 함수 래핑하지만 이는 기본적으로 됨으로 수동으로 래핑하지 마라!!

3.2 래퍼 박싱

스크립트에서 알아서 해줌 -> 왠만하면 직접 하지마...

3.3 언박싱

valueOf를 사용해라

3.4 네이티브, 나는 생성자다

YeonjuOHYE commented 5 years ago

symbol

let id1 = Symbol("id");
let id2 = Symbol("id");

alert(id1 == id2); // false

hidden 값으로 사용될 수 있다

왜냐 고유값이므로 이를 키로 쓰면 이 값을 모르는 third party는 키를 절대 모르므로 값을 가져올 수 없다

let user = { name: "John" };
let id = Symbol("id");

user[id] = "ID Value";
alert( user[id] ); // we can access the data using the symbol as the key
</script>

for문에서 skip 됨

let id = Symbol("id");

let user = {
  name: "John",
  [id]: 123 // not just "id: 123"
};

Globally

// read from the global registry
let id = Symbol.for("id"); // if the symbol did not exist, it is created

// read it again (maybe from another part of the code)
let idAgain = Symbol.for("id");

// the same symbol
alert( id === idAgain ); // true
// get symbol by name
let sym = Symbol.for("name");
let sym2 = Symbol.for("id");

// get name by symbol
alert( Symbol.keyFor(sym) ); // name
alert( Symbol.keyFor(sym2) ); // id

System symbols

오브젝트 파인튜닝 하는데 사용될 수 있다..! https://docs.w3cub.com/javascript/global_objects/symbol/hasinstance/ https://jaeyeophan.github.io/2017/04/20/ES6-8-Symbol/