let empty = {} //property가 없는 객체
let point = {x:0 , y:0} //두개의 숫자 property를 같는 객체
let p2 = {x : point.x , y: point.y+1} //값에 수식이 포함될수있음
let book = {
"주 제목 : "자바 스크립트", //이름에 공백이 포함되면 문자열로처리
"보조-제목 : "개발자를 위한", //이름에 '-'가 포함되어도 문자열로 처리
for : "중급 이상의 개발자", //그외에는 예약어도 문자열로 표시할 필요없음
저자 : { //값이 또다른 객체일경우
firstname : "길동",
surname : "홍"
}
};
function Rectangle(w,h){
this.width = w;
this.height = h;
this.area = function() { return this.width * this.height;}
this.perimeter = function() { return 2 * (this.width + this.height);}
const r1 = new Rectangle(10,20);//객체 생성
const r2 = new Rectangle(15,15);//하나의 생성자를 이용해 여러개의 객체 생성
}
let o1 = Object.creat({x:1 , y:2}) //객체의 생성 Object.create
o1.x + o1.y
const person = {
name : "Adam",
printIntroduction : function() {
console.log(`내이름은 ${this.name} 이고 나이는 ${this.age}세 입니다.`);
}
}
const me = Object.create(person);
me.name = "홍길동" //객체의 property를 overwriting 하거나 추가할수있다
me.age = 25 // age 속성을 추가
me.printIntroduction()
let o = {id : 100 , name:"홍길동};
console.log(o.id , o["name"]); //객체[property] 방식에서 property는 문자열로 표시 해야한다 ""
let customer = {고객1 : "서울", 고객2: "대구", 고객3: "부산", 고객4: "춘천"}, addr="";
for(let i = 1 ; i<3 ;i++)
addr += customer[`고객${i}`] + "";
console.log(addr);
let grade= {학생1: 90, 학생2: 85, 학생3: 94, 학생4: 82}, total=0;
for (student in grade) // 객체이므로 for/in을 사용해야함
total += grade[student]
console.log("평균 = " + String(total / Object.keys(grade).length)); //Object.keys를 통해 객체 기에대한 접근도 가능
Object.create()를 여러번 호출할 경우 객체들 사이의 prototype.chain이 생성이 된다
하위 객체는 상위 prototype의 property를 공유 하위 객체에서 property를 overriding 할 경우 공유 관계는 제거
let o = {}; // 빈 객체 생성
o.x = 1; // o에 own property x를 추가
let p = Object.create(o); // p는 o의 property(= x)를 상속
p.y = 2; // p는 own property y를 추가
let q = Object.create(p); // q는 o와 p의 property들을 상속
q.z = 3; // q는 own property z를 추가
console.log(q, q.x, q.y, o.x, p.y); // { z: 3 } 1 2 1 2 own property만 출력
q.x = 31;
console.log(q, q.x, q.y, o.x, p.y); // { z: 3, x: 31 } 31 2 1 2
p.y = 21;
console.log(q, q.x, q.y, o.x, p.y); // { z: 3, x: 31 } 31 21 1 21
// q = Object.create(p) 실행 후, p의 property가
// 변경되면 q는 그 영향을 받을 수 있다!
let o = {}; // 빈 객체 생성
o.x = 1; // o에 own property x를 추가
let p = Object.create(o); // p는 o의 property(= x)를 상속
p.y = 2; // p는 own property y를 추가
let q = Object.create(p); // q는 o와 p의 property들을 상속
q.x = 31
q.z = 3
delete q.z;
delete q.y;
console.log(q, q.x, q.y, o.x, p.y); // { x: 31 } 31 2 1 2
delete p.y; // p에서 own property를 삭제
console.log(q, q.x, q.y, o.x, p.y); // { x: 31 } 31 undefined 1 undefined
let o = {x : 1}
"x" in o //true
"y" in o //false
"toString" in o //true
o.hasOwnProperty("x") // true: o는 own property "x"를 갖는다
o.hasOwnProperty("y") // false: o에는 property "y"가 없다
o.hasOwnProperty("toString") // false: o는 toString은 상속된 property
o.propertyIsEnumerable("x") // true: own property
o.propertyIsEnumerable("toString") // false: not own property
Object.prototype.propertyIsEnumerable("toString") // false: 열거 불가
let o = { x:1 , y:2 , z:3};
o.propertyIsEnumerable("toString") //false : not enumerable
for(let p in o)
console.log(p) //x,y,z
for(let p in o)
if(!o.hasOwnProperty(p)) continue; //상속된 property는 skip
let target = {x:1} , source = {y:2 , z:3}
for(let key of Object.keys(source))
target[key] = source[key]
console.log(target) // {x:1 , y:2 , z:3}