본문 바로가기

개발

[javaScript] Object (TIL)

728x90

 

 

1. 생성한 오브젝트에 접근하는 방법

 

const player = {
    name : 'julie', 
    points : 10
};

// 프로퍼티에 접근하는 두가지 방법
console.log(player.name);
console.log(player["name"]);

 

2. 오브젝트의 프로퍼티는 const형태로 오브젝트가 선언되어도 변경할 수 있다.

const player = {
    name : 'julie', 
    points : 10
};

console.log( "  Before Change : " + player);
console.log(  player);

player.points = 100;

console.log("  After Change : "  + player);
console.log(  player);

추가로 알게된 내용은 -> 오브젝트 자체에 String 값이랑 더하기 하여 로그를 찍으니 오브젝트 형태로만 찍히고

단독으로 player처럼 찍었을 때처럼 나오지 않았당...

 

2-1 단, Const인 오브젝트 자체를 업데이트 하려는 경우는 에러 발생

const player = {
    name : 'julie', 
    points : 10
};

player = false;
console.log(  player);

오브젝트 자체를 변경하려는 경우는 말 그대로 변경하지 못하도록 const로 선언했기 때문에 변경 불가능

오브젝트 내의 프로퍼티 자체는 오브젝트 자체를 변경하는 것과는 다르기 때문에 변경 가능하다...!

 

3. 프로퍼티는 추가가 가능하다

const player = {
    name : 'julie', 
    points : 10
};

console.log(player);
player.color = "blue";
console.log(player);

 

color프로퍼티를 추가하기 전과 후를 보면 추가된 프로퍼티를 확인 할 수 있다..!

 

728x90