【发布时间】:2013-08-13 05:45:12
【问题描述】:
我正在使用基于此处列出的轻量级插件模式的 JQuery 开发客户端。
我一直在处理一个文件,但由于超过 1000 行代码,它变得臃肿不堪。所以我决定拆分脚本,但我找不到使用 jQuery 保存多个脚本的最佳实践。
我的主要脚本如下:
;(function($, window, document, undefined) {
function MainClass(){
this.other = new Otherclass(); // Otherclass is defined in separate script
}
MainClass.prototype = {
...
}
$.fn.mainclass = function(){
...
}
})(jQuery, window, document);
HTML 如下:
<html>
<head>
// JQuery included
<script type="text/javascript" src="mainclass.js></script>
<script>
$(function() {
$("body").mainclass();
});
</script>
</head>
</html>
问题:我需要在单独的文件中定义 otherclass。实现这一目标的最佳方法是什么?如果插件模式不打算有多个脚本,还有其他适合这个的做法吗?
谢谢。
【问题讨论】:
标签: javascript jquery code-organization plugin-pattern