-
Notifications
You must be signed in to change notification settings - Fork 14
/
README
37 lines (29 loc) · 1022 Bytes
/
README
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
Proto.js – prototypes as classes
The core idea of Proto.js is that many things become simpler in JavaScript if you make the prototype the class and not the constructor.
Details: http://www.2ality.com/2011/06/prototypes-as-classes.html
Code:
// Superclass
var Person = Proto.extend({
constructor: function (name) {
this.name = name;
},
describe: function() {
return "Person called "+this.name;
}
});
// Subclass
var Employee = Person.extend({
constructor: function (name, title) {
Employee.super.constructor.call(this, name);
this.title = title;
},
describe: function () {
return Employee.super.describe.call(this)+" ("+this.title+")";
}
});
Interaction:
var jane = Employee.new("Jane", "CTO"); // normally: new Employee(...)
> Employee.isPrototypeOf(jane) // normally: jane instanceof Employee
true
> jane.describe()
'Person called Jane (CTO)'