Skip to content

Instantly share code, notes, and snippets.

@wowiwo1
Last active January 6, 2017 03:36
Show Gist options
  • Save wowiwo1/31ea59dc95325b6af2af07d997c9347c to your computer and use it in GitHub Desktop.
Save wowiwo1/31ea59dc95325b6af2af07d997c9347c to your computer and use it in GitHub Desktop.
var http = require("http")
// var util = require("util")
var EventEmitter = require("events").EventEmitter
//============ version 1 ==============
var Ticker = function () {
setInterval(()=>{
console.log("tick")
this.emit("tick")
}, 1000)
}
EventEmitter.call(Ticker.prototype)
Object.setPrototypeOf(Ticker.prototype, EventEmitter.prototype)
// util.inherits(Tick, EventEmitter) // 동일한 표현
var ticker = new Ticker()
ticker.on("tick", ()=>{
console.log("tock")
})
//============ version 2 ==============
var ticker = Object.create(new EventEmitter())
setInterval(()=>{
console.log("tick")
ticker.emit("tick")
}, 1000)
ticker.on("tick", ()=>{
console.log("tock")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment