-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.html
145 lines (109 loc) · 3.4 KB
/
test.html
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<script>
//This is just for the purposes of this test file; in a real application you should use an AMD loader
//such as this: https://github.com/cujojs/curl
var simpleoo;
function define(dependencies, definition) {
simpleoo = definition();
}
</script>
<script src="simpleoo.js"></script>
<script>
var log = console.log.bind(console);
var extend = simpleoo.extend;
var mixin = simpleoo.mixin;
var deepCopy = simpleoo.deepCopy;
function Pet(name) {
this.name = name || null;
}
Pet.prototype = {
eat: function() { console.log('yum'); }
};
//instanceof will still work correctly without this, but this gives nicer output in the console;
//if we do console.log(myPet) it will say 'Pet' instead of just 'Object'
//Pet.prototype.constructor = Pet;
var pet = new Pet('test');
log(pet);
function Cat() {
Pet.apply(this, arguments);
}
Cat.prototype = extend(Pet.prototype, {
//constructor: Cat,
//This would be ignored...one might think that simpleoo
//could just assume that the constructor property on the last parameter should
//be the one to use, but that might not be a valid assumption, e.g.:
//
// function ZombieCat() {}
// ZombieCat.prototype = extend(Cat.prototype, Zombie.prototype);
//
//In this the constructor should be set to ZombieCat, not Zombie, as would happen
//if we used the constructor property of the last argument given (in this case Zombie.prototype)
//eat: function() { console.log('overridden eat'); },
meow: function() { console.log('meow'); }
});
Cat.prototype.constructor = Cat;
var garfield = new Cat('Garfield');
log(garfield.name);
log(garfield);
garfield.eat();
garfield.meow();
log(garfield instanceof Cat); //true
log(garfield instanceof Pet); //true
function Person() {
}
function Student() {}
Student.prototype = extend(Person, {
study: function() { console.log('study'); }
});
function Employee() {}
Employee.prototype = extend(Person, {
work: function() { console.log('work'); }
});
var fred = new Person();
//Fred is both a student and an employee
mixin(fred, Student.prototype, Employee.prototype);
//only works in browsers supporting ECMAScript 5 or above
//for older browsers you can try the non-standard fred.__proto__
if (Object.getPrototypeOf) {
log( Object.getPrototypeOf(fred) ); //Person
}
fred.study();
fred.work();
log(fred instanceof Person); //true
//This is false, because Javascript only has a single prototype property,
//and it was already set to Person.prototype
log(fred instanceof Employee); //false
//Order of precedence
var a = {x: 'a'};
var b = {x: 'b'};
var c = {x: 'c'};
mixin(a, b, c);
log(a.x); //c
function A() {}
A.prototype.x = 'a';
function B() {}
B.prototype = extend(A.prototype, {x: 'b'});
function C() {}
C.prototype = extend(B.prototype, {x: 'c'});
var c = new C();
log(c.x); //c
//deepCopy
//add an array property so the test includes that...
garfield.favoriteFoods = ['lasagna', 'hamburgers', 'doughnuts', 'cake'];
var garfieldClone = deepCopy(garfield);
log(garfieldClone);
// deepCopy cyclic
var copyme = {};
var child = {};
var grandchild = {};
child.parent = null;
child.child = grandchild;
grandchild.parent = child;
grandchild.child = null;
copyme.thing = child;
log(copyme);
var copied = deepCopy(copyme);
log(copied);
//ensure that the cyclic-referenced object was cloned correctly
log(copied.thing.child.parent !== child); //true
log(copied.thing.child.parent === copied.thing); //true
</script>