forked from felixge/nodeguide.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_oriented_programming.pdc
More file actions
69 lines (51 loc) · 1.76 KB
/
object_oriented_programming.pdc
File metadata and controls
69 lines (51 loc) · 1.76 KB
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
% Felix's Node.js Object Oriented Programming Guide
# IMPORTANT: This guide is not ready to yet, please go away.
Object oriented programming is a fairly well-understood approach to write
maintainable Software. Even so JavaScript does not explicetly favor OOP, it
makes it easy enough to use it.
To help with some common tasks, I recommend that you use the 'oop' module.
(Disclaimer: I'm the author of the oop module.)
## Basic Example
Here is a basic example of a node.js class. The later parts of this document
will explore various concepts behind it.
var oop = require('oop');
var EventEmitter = require('events').EventEmitter;
function KitchenTimer(properties) {
this._interval = null;
this._timeout = null;
this._minutes = null;
this._start = null;
oop.inherits(this, EventEmitter);
oop.mixin(this, properties);
}
KitchenTimer.SECOND = 1000;
KitchenTimer.MINUTE = KitchenTimer.SECOND * 60;
KitchenTimer.create = function(minutes) {
var timer = new KitchenTimer();
timer.set(minutes);
return timer;
}
KitchenTimer.prototype.set = function(minutes) {
var ms = minutes * KitchenTimer.MINUTE;
this._timeout = setTimeout(this._ring.bind(this), ms);
this._interval = setInterval(this._tick.bind(this), KitchenTimer.SECOND);
this._minutes = minutes;
this._start = new Date();
};
KitchenTimer.prototype._tick = function() {
this.emit('tick');
};
KitchenTimer.prototype._ring = function() {
this.emit('ring');
this.reset();
}
KitchenTimer.prototype.reset = function() {
clearTimeout(this._timeout);
clearInterval(this._interval);
oop.reset(this);
}
KitchenTimer.prototype.remaining = function() {
var ms = (new Date() - this._start);
var minutes = ms / KitchenTimer.MINUTE;
return minutes;
};