jQuery 模式 - 这是有效的还是有更好的方法?
我已经陷入了这种 javascript 组织中,并且想知道我是否错过了这里的要点,或者是否有更优雅的方法来做到这一点。
基本上,我将所有内容包装在函数(对象)中,然后在该对象上设置方法,然后实例化包装器对象的实例并传入任何选项和依赖项。
我预感有一种方法可以自动运行 .init()
以及一些其他可以进行的调整。我做对了吗?
function AppModuleCore(){
var AppModuleCore = this; //keep internals sane
// Various global vars, objects
AppModuleCore.defaultOptions = {};
AppModuleCore.init = function(opts) {
// todo: that thing where you extend an options object a la juery
AppModuleCore.bindEvents();
};
AppModuleCore.bindEvents = function() {
// bind events here, send to functions within AppModuleCore.<FUNCTIONNAME>();
// Example:
$("a#clicker").unbind("click");
$("a#clicker").click(function(event){
AppModuleCore.handleClickerClick(event);
});
};
AppModuleCore.handleClickerClick = function(event){
alert("clicker was clicked");
};
}
// --------------------------------------------------------------------
// instantiate AppModuleCore object and initialize with opts,
// dependency injection
// --------------------------------------------------------------------
$(document).ready(function(){
AppModuleCore = new AppModuleCore;
var options = {};
AppModuleCore.init(options);
});
I've sort if fell into this organization of javascript and was wondering if I'm missing the point somewhere here, or if there's a more elegant way of doing this.
Basically I'm wrapping everything in a function (object) and then setting up methods on that object, then instantiating an instance of the wrapper object and passing in any options and dependencies.
I have a hunch there's a way to automatically run .init()
and a few other tweaks that could be made. Am I doing it right?
function AppModuleCore(){
var AppModuleCore = this; //keep internals sane
// Various global vars, objects
AppModuleCore.defaultOptions = {};
AppModuleCore.init = function(opts) {
// todo: that thing where you extend an options object a la juery
AppModuleCore.bindEvents();
};
AppModuleCore.bindEvents = function() {
// bind events here, send to functions within AppModuleCore.<FUNCTIONNAME>();
// Example:
$("a#clicker").unbind("click");
$("a#clicker").click(function(event){
AppModuleCore.handleClickerClick(event);
});
};
AppModuleCore.handleClickerClick = function(event){
alert("clicker was clicked");
};
}
// --------------------------------------------------------------------
// instantiate AppModuleCore object and initialize with opts,
// dependency injection
// --------------------------------------------------------------------
$(document).ready(function(){
AppModuleCore = new AppModuleCore;
var options = {};
AppModuleCore.init(options);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,一些要点
时,将代码包装在构造函数中才有意义。
您的代码不会表现出这些特征。我这样说是因为您的 jQuery 选择器
a#clicker
是硬编码的,所以我假设您不想将相同的事件多次绑定到它们?你最好使用一个函数(也许是你的 init )或一个对象文字来限制你的范围。
在风格上:当你使用与构造函数相同的名称来别名
this
,然后添加方法到别名,乍一看就像您正在向构造函数添加静态方法。这可能会让稍后查看您的代码但没有注意到别名的人感到困惑。OK, some points
Having your code wrapped in a constructor only really makes sense if
Your code doesn't exhibit these characteristics. I say this because your jQuery selectors
a#clicker
are hard coded so I'm assuming that you wouldn't want to bind the same events to them more than once?You'd be better off using a function (perhaps your init) or an object literal to limit your scope..
On a stylistic note: when you alias
this
with the same name as the constructor and then add methods to the alias, it looks at first glance like you are adding static methods to the constructor. This may be confusing to someone who looks at your code later and doesn't notice the alias.