Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- console.clear();
- var cl = function(text){ console.log(text); }
- var Users = {
- list: [],
- listeners: {},
- add: function(name) {
- this.list.push({name: name});
- this.dispatch("user-added");
- },
- on: function(eventName, listener) {
- if(!this.listeners[eventName]) this.listeners[eventName] = [];
- this.listeners[eventName].push(listener);
- },
- dispatch: function(eventName) {
- if (this.listeners[eventName]) {
- var i, max = this.listeners[eventName].length;
- for (i = 0; i < max; ++i){
- this.listeners[eventName][i](this);
- }
- }
- },
- numOfAddedUsers: function() {
- return this.list.length;
- }
- }
- Users.on(
- "user-added",
- function(){
- cl(Users.numOfAddedUsers());
- }
- );
- Users.add("Casimir");
- Users.add("Zygmunt");
Add Comment
Please, Sign In to add comment