-
Notifications
You must be signed in to change notification settings - Fork 75
/
Prototype.js
66 lines (54 loc) · 1.55 KB
/
Prototype.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict';
class Prototype {
constructor(prototype) {
console.log("Prototype Class created");
}
setFeature(key, val) {
this[key] = val
}
clone() {
console.log("Prototype.clone invoked");
}
}
class ConcretePrototype1 extends Prototype {
constructor() {
super();
console.log("ConcretePrototype1 created");
this.feature = "feature 1"
}
clone() {
console.log('ConcretePrototype1.clone invoked');
let clone = new ConcretePrototype1();
let keys = Object.keys(this);
keys.forEach(k => clone.setFeature(k, this[k]));
console.log("ConcretePrototype1 cloned");
return clone;
}
}
class ConcretePrototype2 extends Prototype {
constructor() {
super();
console.log("ConcretePrototype2 created");
this.feature = "feature 2"
}
clone() {
console.log('ConcretePrototype2.Clone function');
let clone = new ConcretePrototype2();
let keys = Object.keys(this);
keys.forEach(k => clone.setFeature(k, this[k]));
console.log("ConcretePrototype2 cloned");
return clone;
}
}
var proto1 = new ConcretePrototype1();
proto1.setFeature('feature', "feature 11");
var clone1 = proto1.clone();
console.log(clone1.feature);
console.log(typeof clone1);
console.log(clone1 === proto1);
var proto2 = new ConcretePrototype2();
proto2.setFeature('feature', "feature 22");
var clone2 = proto2.clone();
console.log(clone2.feature);
console.log(typeof clone2);
console.log(clone2 === proto2);