-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctional_inheritance.js
More file actions
36 lines (32 loc) · 874 Bytes
/
functional_inheritance.js
File metadata and controls
36 lines (32 loc) · 874 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
// Inheritance this way is much simpler than either pseudoclassical
// or prototypal, but, there is a problem.
function gizmo(id) {
return {
id: id,
toString: function () {
return "gizmo " + this.id;
}
};
}
function hoozit(id) {
var that = gizmo(id); // we use that because this is a reserved word.
that.test = function (testid) {
return testid === this.id;
};
return that;
}
// Gizmo and Hoozit objects' ids are public! Anybody who wants to can mess with them!
// What to do? Simple! Make the ids private! The code even gets simpler in the process.
function gizmo(id) {
return {
toString: function () {
return "gizmo " + id;
};
}
function hoozit(id) {
var that = gizmo(id);
that.test = function (testid) {
return testid === id;
};
return that;
}