Last active
May 22, 2017 15:10
-
-
Save pjchender/08a18897536eb3b66c22e4743695733a to your computer and use it in GitHub Desktop.
[Node][Unit36] 使用 util.inherits 來繼承 EventEmitter 的方法
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
// 使用 util.inherits 來繼承 EventEmitter 的方法 | |
const EventEmitter = require('events') | |
const util = require('util') | |
function Greetr () { | |
// 如果我們想要完整繼承 EventEmitter 中的屬性 | |
EventEmitter.call(this) | |
this.greeting = 'someone greet' | |
} | |
// inherits(constructor, superConstructor), 讓 constructor 繼承 superConstructor | |
util.inherits(Greetr, EventEmitter) | |
Greetr.prototype.greet = function () { | |
console.log(this.greeting) | |
this.emit('greet') | |
} | |
const greetr1 = new Greetr() | |
greetr1.on('greet', function () { | |
console.log('Hello world') | |
}) | |
greetr1.greet() |
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
// 在 eventEmitter 中代入參數 | |
const EventEmitter = require('events') | |
const util = require('util') | |
function Greetr () { | |
this.greeting = 'Hello World!!' | |
} | |
// inherits(constructor, superConstructor), 讓 constructor 繼承 superConstructor | |
util.inherits(Greetr, EventEmitter) | |
Greetr.prototype.greet = function (data) { | |
console.log(this.greeting) | |
this.emit('greet', data) | |
} | |
const greetr1 = new Greetr() | |
greetr1.on('greet', function (data) { | |
console.log('Someone greeted' + ': ' + data) | |
}) | |
greetr1.greet('Nice') |
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
'use strict' | |
const EventEmitter = require('events') | |
const util = require('util') | |
class Greetr { | |
constructor () { | |
EventEmitter.call(this) | |
this.greeting = 'someone greet' | |
} | |
greet () { | |
console.log(this.greeting) | |
this.emit('greet') | |
} | |
} | |
util.inherits(Greetr, EventEmitter) | |
const greetr1 = new Greetr() | |
greetr1.on('greet', function () { | |
console.log('Hello world') | |
}) | |
greetr1.greet() |
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
const util = require('util') | |
function Person (firstName, lastName) { | |
this.firstName = firstName || 'Aaron' | |
this.lastName = lastName || 'Chen' | |
} | |
Person.prototype.greet = function () { | |
console.log('Hello, ' + this.firstName + ' ' + this.lastName) | |
} | |
function policeMan () { | |
console.log('this', this) | |
Person.call(this) | |
/** | |
* 等同於執行了 | |
* this.firstName = 'Aaron' | |
* this.lastName = 'Chen | |
*/ | |
this.badgeNumber = '1234' | |
} | |
util.inherits(policeMan, Person) | |
let officer = new policeMan() | |
console.log('officer', officer) | |
officer.greet() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
由於 `util.inherit(constructor, superConstructor) 只會幫我們繼承 superConstructor.prototype 中的內容,所以如果我們想要完整的繼承,也就是也繼承 superConstructor 中的所有屬性,我們要在 constructor 中去呼叫 superConstructor.call(this),像這樣: