Last active
April 24, 2017 09:44
简单的发布订阅者模式的实现
This file contains 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
var Observer = function () { | |
}; | |
Observer.prototype = { | |
constructor: Observer, | |
subscribe: function (eventName, func) { | |
if (!this[eventName]) this[eventName] = []; | |
this[eventName].push(func); | |
}, | |
publish: function (eventName, data) { | |
var _this = this; | |
if (!this[eventName]) this[eventName] = []; | |
this[eventName].forEach(function (func) { | |
func.call(_this, data); | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使用: