我有这个示例模块,我想在不进行单独调用的情况下进行初始化,而是在消费者页面中有一个数组,这样每个页面都可以有一个带有所需模块名称的小数组。
我已经这样做了,但它没有调用模块:
var navModule = (function() {
var nav = {};
var navHTMLobjs = {
navList: $('#nav'),
listItems: $('#nav').find('li'),
listLinks: $('#nav').find('a')
};
nav.bindOver = function() {
navHTMLobjs.navList.on('mouseover mouseout', 'li a', function(e) {
if (e.type == 'mouseover') {
$(this).addClass('over');
}
if (e.type == 'mouseout') {
$(this).removeClass('over');
}
});
};
nav.isOverBinded = function() {
return navHTMLobjs.navList.data('events').hasOwnProperty('mouseover') && navHTMLobjs.navList.data('events').hasOwnProperty('mouseout');
};
nav.turnOff = function() {
navHTMLobjs.navList.off('mouseover mouseout');
};
nav.isNavTurnedOff = function() {
return !navHTMLobjs.navList.data.hasOwnProperty('events');
};
nav.init = function() {
this.bindOver();
};
return nav;
});
//var myNav = new navModule($('#nav'));
//myNav.init();
// So I want to only include this array which will be written by
//back end and each page will only have its required bits.
var pageModules = ['navModule'];
for (var m in pageModules) {
var fn = window[pageModules[m]];
//var fn = Function(pageModules[m]); This says navModule is not defined :(
if (typeof fn === 'function') {
var inner = new fn();
inner.init();
}
}
当前返回 fn 为未定义
也让我知道这是否是解决此问题的正确方法,如果不是,也许是一个建议。
谢谢