HTML and CSS Reference
In-Depth Information
Listing 14.54 Expecting chatRoom to be event emitter
testCase(exports, "chatRoom", {
"should be event emitter": function (test) {
test.isFunction(chatRoom.addListener);
test.isFunction(chatRoom.emit);
test.done();
}
});
We can pass this test by popping EventEmitter.prototype in as chat-
Room 's prototype, as seen in Listing 14.55.
Listing 14.55 chatRoom inheriting from EventEmitter.prototype
/* ... */
var EventEmitter = require("events").EventEmitter;
/* ... */
var chatRoom = Object.create(EventEmitter.prototype);
chatRoom.addMessage = function (user, message) {/* ... */};
chatRoom.getMessagesSince = function (id) {/* ... */};
Note that because V8 fully supports ECMAScript 5's Object.create ,we
could have used property descriptors to add the methods as well, as seen in
Listing 14.56.
Listing 14.56 chatRoom defined with property descriptors
var chatRoom = Object.create(EventEmitter.prototype, {
addMessage: {
value: function (user, message) {
/* ... */
}
},
getMessagesSince: {
value: function (id) {
/* ... */
}
}
});
 
Search WWH ::




Custom Search