Created
January 7, 2016 07:03
-
-
Save toanalien/203800eede6e2d71de0b to your computer and use it in GitHub Desktop.
Inheriting from Event Emitter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* inherits.js | |
* @toanalien | |
*/ | |
var util = require('util'); | |
var EventEmitter = require('events'); | |
function Toanalien() { | |
this.toanalien = "Hello !"; | |
} | |
util.inherits(Toanalien, EventEmitter); // lay properties cua EventEmitter cho vao Toanalien | |
Toanalien.prototype.toan = function() { // khoi tao prototype 'toan' | |
console.log(this.toanalien); | |
this.emit('toan'); // goi event 'toan' | |
} | |
var Toan = new Toanalien(); | |
Toan.on('toan', function() { | |
console.log('Hi !'); | |
}); | |
Toan.toan(); // goi method 'toan' | |
/* | |
* output | |
* Hello ! | |
* Hi ! | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment