The*_*ght 9 html javascript jquery module-pattern
我有以下代码:
filtersManager = (function ($) {
var that = this;
function configure() {
// some work
return that;
};
function process() {
// some work
return that;
}
return {
// public functions
configure: configure,
process: process
};
}(jQuery));
Run Code Online (Sandbox Code Playgroud)
但是当使用下面的方法调用它时会失败:
filtersManager.configure().process();
Error: Object doesn't support property or method 'process'
Run Code Online (Sandbox Code Playgroud)
而以下工作:
filtersManager.configure();
filtersManager.process();
Run Code Online (Sandbox Code Playgroud)
ale*_*lex 15
你正在返回错误的东西(this在普通的函数调用中是全局对象).您想要返回最初创建的对象,我将其称为接口.
filtersManager = (function ($) {
var interface = {
// public functions
configure: configure,
process: process
};
function configure() {
// some work
return interface;
};
function process() {
// some work
return interface;
}
return interface;
}(jQuery));
Run Code Online (Sandbox Code Playgroud)
如果您想知道为什么我可以参考下面定义的功能,那就是由于提升.