Hi I\'m tyring to implement observer pattern in JavaScript:
My index.js:
$(document).ready(function () {
var ironMan = new Movie();
The observer pattern is all about updating an object and having those updates automatically send out an event that gives info about what was updated.
For example:
function ObserverList(){
this.observerList = []
this.listeners = []
}
ObserverList.prototype.add = function( obj ){
this.observerList.push(obj)
this.listeners.forEach(function(callback){
callback({type:'add', obj:obj})
})
}
ObserverList.prototype.onChange = function(callback){
this.listeners.push(callback)
}
Here's a module of the observer pattern in javascript, you can take a look at the source code fore more info: https://github.com/Tixit/observe