JavaScript 揭示模块模式重构建议
我在当前的项目中经常使用 JavaScript 的揭示模块模式来组织我的代码,但我想知道我是否可以以更好的方式使用它,因为我似乎创建了很多私有方法,然后以线性方式调用它们,即当一个方法执行完其操作后,调用另一个方法,然后调用另一个方法,依此类推。
该代码可以工作,但想看看它是否可以更好、更高效或使用更少的代码来实现相同的结果。
下面是一个示例模块,我们需要:
- 将点击事件绑定到选项卡
- 设置一些元数据值
- 调用第 3 方跟踪函数
将元数据重置为其原始值
icisSite.tabPageView = 函数($){ var $tabs = $('.tbc .tbc-tabs > div'), 页面名称 = $('元[名称=gwa_pageName]'), sec3 = $('元[名称=gwa_siteSection3]'), sec4 = $('元[名称=gwa_siteSection4]'), 页名长度 = 页名.长度, sec3Len = sec3.长度, sec4Len = sec4.长度, TXT, pName内容, pName构造, 部分, 元内容, 元构造; 函数初始化(){ bindClickHandlerToTabs(); }; 函数bindClickHandlerToTabs() { if (pageNameLen !== 0 && (sec3Len !== 0 || sec4Len !== 0)){ $tabs.bind('点击', updateMetaData(){ txt = $(this).find('span').text(), txt='| ' + 文本; 更新元数据(); }); } 别的 { 返回假; } }; 函数更新元数据(){ pNameContent = pageName.attr('内容'), pNameConstruct = pNameContent + txt; if (sec3Len !== 0 && sec4Len !== 0){ 部分 = 'gwa_siteSection4', metaContent = sec4.attr('内容'), 元构造=元内容+txt; } 别的 { 部分 = 'gwa_siteSection3' 元内容 = sec3.attr('内容'), 元构造=元内容+txt; } 返回调用GWATrackingFucntions(); }; 函数调用GWATrackingFucntions() { gwa_SetMetaValue(节,metaConstruct); gwa_trackPageView(pNameConstruct); 返回重置元数据(); }; 函数重置元数据(){ pageName.attr('内容', pNameContent); $('元').each(函数(索引){ var name = $(this).attr('name'); if (部分 === 名称){ $(this).attr('内容', 元内容); } }); }; 返回 { 初始化:初始化 }; }(jQuery); icisSite.tabPageView.init();
I am using the revealing module pattern with JavaScript quite alot to organise my code in my current project but am wondering if i could use it in a better way as i seem to be creating lots of private methods and then calling them in a linear fashion i.e. when one method has performed its operation then call another and then another and so on.
The code works but would like to see if it could work better, more efficiently or with less code to achieve the same result.
An example module would be below where we need to:
- Bind a click event to a tab
- set some meta data values
- Call 3rd party tracking functions
Reset the meta data to its original value
icisSite.tabPageView = function($){ var $tabs = $('.tbc .tbc-tabs > div'), pageName = $('meta[name=gwa_pageName]'), sec3 = $('meta[name=gwa_siteSection3]'), sec4 = $('meta[name=gwa_siteSection4]'), pageNameLen = pageName.length, sec3Len = sec3.length, sec4Len = sec4.length, txt, pNameContent, pNameConstruct, section, metaContent, metaConstruct; function init() { bindClickHandlerToTabs(); }; function bindClickHandlerToTabs() { if (pageNameLen !== 0 && (sec3Len !== 0 || sec4Len !== 0)){ $tabs.bind('click', updateMetaData(){ txt = $(this).find('span').text(), txt = ' | ' + txt; updateMetaData(); }); } else { return false; } }; function updateMetaData(){ pNameContent = pageName.attr('content'), pNameConstruct = pNameContent + txt; if (sec3Len !== 0 && sec4Len !== 0){ section = 'gwa_siteSection4', metaContent = sec4.attr('content'), metaConstruct = metaContent + txt; } else { section = 'gwa_siteSection3' metaContent = sec3.attr('content'), metaConstruct = metaContent + txt; } return callGWATrackingFucntions(); }; function callGWATrackingFucntions() { gwa_SetMetaValue(section, metaConstruct); gwa_trackPageView(pNameConstruct); return resetMetaData(); }; function resetMetaData() { pageName.attr('content', pNameContent); $('meta').each(function(index){ var name = $(this).attr('name'); if (section === name){ $(this).attr('content', metaContent); } }); }; return { init: init }; }(jQuery); icisSite.tabPageView.init();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要创建仅调用一次的函数,则需要询问为什么它们是函数而不仅仅是顺序代码。也许是为了可扩展性或模块化。额外的函数调用并不是特别重要。
您可以通过更改以下内容中固有的多个函数调用来极大地提高效率(尽管可能不明显):
使用直接属性访问:
除非您的目的是修改 HTML 内容属性(这不太可能)而不是 DOM 属性(我有不知道在这种情况下,attr 方法实际上会改变哪一个)。
jQuery 即将对饱受诟病的 attr 方法进行一些重大更改,除非您确实需要(几乎从来没有),否则不要使用它始终是一个好主意。
If you are creating functions that are only called once, you need to ask why they are functions and not just sequential code. Perhaps it's for extensibility or modularity. The extra function call is not particularly important.
You can greatly improve efficiency (though probably not noticeably) by changing the multiple function calls inherent in things like:
with direct property access:
unless your intention is to modify the HTML content attribute (which is unlikely) rather than the DOM property (I have no idea which one the attr method will actually change in this case).
jQuery is about to make some big changes to the much maligned attr method, it's always been a good idea to not use it unless you really needed to (which was almost never).