Created
July 20, 2017 16:59
-
-
Save Sara3/6cca7239911ff956ab366571ec196c01 to your computer and use it in GitHub Desktop.
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 messageBus = { | |
subscrib: function(str, callback) { | |
// make a key in the parent object | |
if (!this[str]) { | |
this[str] = []; | |
this[str].push(callback) | |
} else { | |
this[str].push(callback) | |
} | |
}, | |
publish: function(str, val) { | |
console.log(str) | |
for ( property in this) { | |
console.log(property) | |
} | |
if(str!=='topic') { | |
return 'no subscriber for that topic'; | |
} | |
for(let i = 0; i < this[str].length; i++) { | |
this[str][i](val); | |
} | |
} | |
} | |
messageBus.subscrib('topic', (arr)=> { | |
console.log('callback called!', arr) | |
}); | |
messageBus.subscrib('topic', (arr)=> { | |
console.log('callback2 called!', arr) | |
}); | |
messageBus.publish('topic', 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment