value는 함수를 포함해 모든 데이터 타입으로 쓸 수 있다.
.(dot notation) 이용하거나
[] (bracket notation) 이용. []안에는 ' ' 표시 해줘야 됨.
object.key
object['property Name'] / object['key']
key에 ' '(qutation mark) 는 object 의 key 에 공백(space)이 포함 되어 있을 경우에 사용.
object는 mutable -> 즉, 새로 값을 할당 할 수 있다.
할당하는 방법은 . (dot notation), [] (bracket notation), = (assignment operator)이 있다.
const 로 선언된 object에 재할당 하는것은 불가능하나, 새로운 property를 더하거나, property를 바꿀 수 는 있다.
const apple = {color: 'red'}
//apple = {color: 'blue'} //이건 재할당 하는거라서 안됨.
apple.color = 'pink' //property를 바꿈.
apple.taste = 'good'
//값 삭제 하는 것도 됨.
delete apple.color //'propert Name' 일 경우에는 delete objectName['Property Name'];
let objectName = {
methodName() {
console.log('The methodName method was invoked!')
},
secondMethodName() {
console.log('The secondMethodName method was invoked!')
}
};
alienShip.methodName(); //호출하기
call by value 단순히 말해서 값을 복사해서 받아 오는것이고,
call by reference는 메모리의 위치(주소값)를 받아오는것 하는것.
let apple = {
color : 'red'
}
let change = (key) => {
key.color = 'yellow'
}
let add = (newThing) => {
newThinng = {color : 'orange'}
}
change (apple)
add(apple)
console.log(apple) // color: yellow
change 함수
1) apple이라는 객체가 존재한다. (주소값: 01)
2) change 함수가 실행되면서 parameter로 apple(주소값:01)을 받아온다.
3) parameter key
는 함수 change의 argument인 apple
객체의 주소값을 복사한다.
4) 같은 주소값(01)을 가지고 있기 때문에 color의 값이 yellow 로 바뀔 수 있다.
add 함수
1) apple 이라는 객체가 존재한다. (주소값: 01)
2) add 함수가 실행되면서 parameter로 apple(주소값:01)을 받아온다
3) add 함수안의 newThing
이라는 변수가 새로운 객체(newThinng = {taste : 'good'})
를 가지게 되면서 새로운 주소 값(02)을 참조한다. (newThing의 주소값: 02)
4) add 함수의 parameter인 newThing
(주소값:02), argument인 apple
(주소값: 01)은 서로 다른 객체(주소값)을 가르키기 때문에 color는 orange로 변경되지 않는다.
const spaceship = {
homePlanet : 'Earth',
color : 'silver'
};
let paintIt = obj => {
obj.color = 'glorious gold'
};
paintIt(spaceship);
spaceship.color // Returns 'glorious gold'
객체 spaceship의 주소값과 함수 paintIt의 parameter인 obj가 가르키는 주소값이 같기때문에 color가 수정된다. (이때 const 에 할당된 객체여도 객체는 영구적으로 바뀐다.)
let spaceship = {
homePlanet : 'Earth',
color : 'red'
};
let tryReassignment = obj => {
obj = {
identified : false,
'transport type' : 'flying'
}
console.log(obj) // Prints {'identified': false, 'transport type': 'flying'}
};
tryReassignment(spaceship) // The attempt at reassignment does not work.
spaceship // Still returns {homePlanet : 'Earth', color : 'red'};
spaceship = {
identified : false,
'transport type': 'flying'
}; // Regular reassignment still works.
tryReassignment
함수의 argument인 spaceship
이라는 객체의 주소값이 tryReassignment
함수의 parameter인 obj
이 생성한 새로운 객체의 주소값과 다르기 때문에 spaceship의 값은 바뀌지 않는다.
object 이용해서 함수 선언하기
let functionName = objectParam => {
objectParam['Property Name'] = 'New Property Value';
};
let functionName = objectParam => {
objectParam.propertyName = 'A Property Value';
};