私は次のコードを持っています:
// 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'" );
}
};
})();
また、独自のイベント処理システムを作成したいと考えています。私は次のコードで試しました:
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');
}
};
})();
そして、私は次のように使用しました:
mySingleton .registerHandler('hello', function () {
alert('Hi!');
});
しかし、これは機能しません...次のエラーをスローしてください:
Cannot read property 'prototype' of undefined
誰でも私を助けることができますか?
私の現在のエラー:
Uncaught TypeError: Cannot set property 'mycustomevent' of undefined
Uncaught TypeError: Cannot read property 'mycustomevent' of undefined