我正在寻找一个JS解决方案/设计模式,允许在init()函数中绑定窗口加载事件多次调用函数C.Test.init()。不幸的是,我不能让这个工作。任何人都可以帮我这个吗?
var C = {};
C.Test = (function(C)
{
var me = {};
me.init = function(config)
{
me._config = config;
$(window).bind('load.' + config.name, me, me.sayHello);
}
me.sayHello = function(e)
{
// this doesn't work:
document.write('HELLO ' + me._config.name + '<br>');
// this doesn't work either:
document.write('SALVE ' + e.data._config.name + '<br>');
}
return {
init : me.init
}
})(C);
C.Test.init({
name: 'John'
});
C.Test.init({
name: 'Kate'
});
这是JS Fiddle链接:http://jsfiddle.net/4Ss8L/