-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-roles.html
61 lines (49 loc) · 1.25 KB
/
test-roles.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
<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;
var extend = simpleoo.extend;
var mixin = simpleoo.mixin;
function Person() {}
Person.prototype.addRoles = function(role1 /*, role2, ... */) {
if (this.initRole) {
throw new Error("addRoles method doesn't work if an initRole method exists on the Person prototype");
}
for(var i in arguments) {
var role = arguments[i];
if (typeof role.initRole=='function') {
role.initRole.call(this);
}
mixin(this, role);
}
delete this.initRole;
};
var studentRole = {
initRole: function() {
console.log('init student');
this.school = null;
this.numCourses = null;
},
study: function() { log('study'); },
}
var employeeRole = {
initRole: function() {
console.log('init employee');
this.employer = null;
},
work: function() { log('work'); }
}
var fred = new Person();
fred.addRoles(studentRole, employeeRole);
fred.school = fred.employer = "Cyberland College";
fred.numCourses = 4;
fred.work();
console.log(fred);
</script>