-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_create.js
45 lines (37 loc) · 1.03 KB
/
object_create.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
let superObject = {superVal: 'super'};
let subObject = Object.create(superObject);
// superObject 를 부모로 하는 새로운 객체 subObject 를 생성한다
subObject.subVal = "subVal";
debugger;
console.log(subObject);
console.log("subObject.subVal => ", subObject.subVal);
console.log("subObject.superVal => ", subObject.superVal);
subObject.superVal = 'changedSuperVal';
console.log("subObject.superVal", subObject.superVal);
console.log("superObject.superVal", superObject.superVal);
let user1 = {
name: 'name',
first: 10,
second: 20,
sum() {
return this.first+this.second;
}
}
let user2 = Object.create(user1);
user2.name = "name2";
user2.first = 30;
user2.second = 4;
user2.avg = function() {
return (this.first+this.second) / 2;
};
// user2 = {
// name: 'user2',
// first: 20,
// second: 30,
// avg() {
// return (this.first+this.second) / 2;
// }
// }
// user2.__proto__ = user1;
console.log('user2.sum(): ', user2.sum());
console.log('user2.avg(): ', user2.avg());