-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementingObjectCreate.js
More file actions
41 lines (35 loc) · 949 Bytes
/
ImplementingObjectCreate.js
File metadata and controls
41 lines (35 loc) · 949 Bytes
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
// prototype - a prototype to be inherited by newly created object
// properties (optional) - argument, to be passed to Object.defineProperties
//
// throws TypeError if `prototype` parameter is not object and is not null
//
// returns newly created object
Object.create = function (prototype, properties) {
console.log(properties);
if (!prototype && prototype !== null || !prototype instanceof Object) {
throw new TypeError()
}
let newObj = {};
newObj.__proto__ = prototype;
if (properties !== undefined) {
Object.defineProperties(newObj, properties)
}
return newObj
};
let citizen = {
sleep: function () {
return "zzZ...";
},
panic: function () {
return "AaAaAaAa!";
}
};
let veteran = Object.create(citizen, {
panic: {
value: function () {
return "SNAFU";
}
}
});
console.log(veteran.sleep());
console.log(veteran.panic());