https://sunshineyellow.tistory.com/17?category=1037372
[코딩앙마 외] 객체(Object)와 프로퍼티(property)
객체(Object) key와 value로 이루어질 수 있다. 1. 함수표현식 const goodCoder = function(name, age, coding) { name : 'Noran', age : 20, coding : function(){ console.log('화이팅!'); } } name : 'Noran',..
sunshineyellow.tistory.com
https://sunshineyellow.tistory.com/18?category=1037372
[코딩앙마 외] 객체 리터럴과 객체 접근법, 그리고 생성자 함수
객체를 생성하는 방법에는 두 가지가 있다. 객체 리터럴 생성자 함수 단 하나의 객체만을 생성할 때는 직관적이고 간편한 객체 리터럴을 사용하고, 같은 객체를 대량생산할 때는 생성자함수를
sunshineyellow.tistory.com
위 글을 보충해 객체 프로퍼티의 생성과 수정, 삭제에 대해 다룬다.
생성
클래스의 객체 생성
const noran = new Goodcoder('noran', 20);
클래스 없이 객체 생성
const noran = { name : 'noran', age : 20 };
수정
key 추가
noran.hasjob = true;
key 삭제
delete noran.hasjob;
cloning
아래의 경우 coder1과 coder2의 value가 같이 바뀐다.
const coder1 = { name : 'noran', age : 20 };
const coder2 = coder1;
coder2.name = 'paran';
console.log(coder1); //'paran'
properties 복사하기 (예전버전)
const coder3 = {};
for (key in coder1) {
coder3[key] = coder1[key];
}
console.log(coder3); //{name : 'noran', age : 20}
properties 복사하기 (신버전)
const coder4 = {};
Object.assign(coder4, coder1);
console.log(coder4); //{name : 'noran', age : 20}
위는 아래처럼 정리될 수 있다.
const coder4 = Object.assign({}, coder1);
출처 : 드림코딩 https://www.youtube.com/@dream-coding / MDN https://developer.mozilla.org/ko
'🟨 JavaScript > 개념' 카테고리의 다른 글
| [mdn, 드림코딩 외] 배열(array) : 배열에의 접근과 looping (0) | 2022.05.31 |
|---|---|
| [mdn, 드림코딩 외] 객체(Object)와 프로퍼티(property) (3) : computed properties (0) | 2022.05.31 |
| [mdn, 드림코딩 외] 객체지향 (5) : instanceOf 연산자 (0) | 2022.05.25 |
| [mdn, 드림코딩 외] 객체지향 (4) : 클래스의 상속 (sub classing) (0) | 2022.05.25 |
| [mdn, 드림코딩 외] 객체지향 (3) : 정적 속성과 메소드(static properties and methods) (0) | 2022.05.04 |