I have the next code:
// Singleton
var mySingleton = (function () {
// Private methods and variables
function privateMethod(){
console.log( "I am private" );
}
var privateVariable = "Im also private";
return {
// Public methods and variables
hello: function () {
console.log( "I want to ''fire'' the event 'hello'" );
}
};
})();
And I want to create my own event handling system. I'm tried with the following code:
var mySingleton = (function () {
function privateMethod(){
console.log( "I am private" );
}
var handlers = {};
mySingleton.prototype.registerHandler = function(event, callback) {
this.handlers[event] = callback;
};
mySingleton.prototype.fire = function(event) {
this.handlers[event]();
};
var privateVariable = "Im also private";
return {
hello: function () {
console.log( "I want to ''fire'' the event 'hello'" );
fire('hello');
}
};
})();
And I used as follows:
mySingleton .registerHandler('hello', function () {
alert('Hi!');
});
But this doesn't work... Throw me the next error:
Cannot read property 'prototype' of undefined
Anybody can help me, please?
MY CURRENT ERROR:
Uncaught TypeError: Cannot set property 'mycustomevent' of undefined
Uncaught TypeError: Cannot read property 'mycustomevent' of undefined