[英]How to use Chain Pattern with Self Revealing Module Pattern in JavaScript?
I have the below code: 我有以下代码:
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));
but when it's called using the below it fails: 但是当使用下面的方法调用它时会失败:
filtersManager.configure().process();
Error: Object doesn't support property or method 'process'
whereas the below works: 而以下工作:
filtersManager.configure();
filtersManager.process();
You are returning the wrong thing ( this
in a plain function invocation is the global object). 你正在返回错误的东西( this
在普通函数调用中是全局对象)。 You want to return the object that you originally created, which I will call the interface . 您想要返回最初创建的对象,我将其称为接口 。
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));
If you're wondering why I can reference the functions defined below, it's due to hoisting. 如果您想知道为什么我可以参考下面定义的功能,那就是由于提升。
Immediate function is executed in global object ( window
) context. 立即函数在全局对象( window
)上下文中执行。 Try something similar to this: 尝试类似的东西:
filtersManager = (function ($) {
var that = {};
that.configure = function() {
// some work
return that;
};
that.process = function() {
// some work
return that;
}
return that;
}(jQuery));
Constructor pattern seems to fit your need better: 构造函数模式似乎更符合您的需求:
var FiltersManager = (function($) {
function FiltersManager() {}
FiltersManager.prototype = {
configure: function() {
console.log('configure');
return this;
},
process: function() {
console.log('process');
return this;
}
}
return FiltersManager;
}(jQuery));
new FiltersManager().configure().process();
As to continue what others have said , I think you confused with the function constructor syntax which would work , similar to what you've said ; 至于继续其他人所说的话,我认为你混淆了函数构造函数语法,它会起作用,类似于你所说的;
var G=function g()
{
this.configure =function (){return this;}
this.process =function (){return this;}
};
var _= new G();
console.log(_.configure().process())
If you wanted to re-use the functions on other objects too, you could do it like this 如果你想在其他对象上重复使用这些函数,你也可以这样做
filtersManager = function ($) { function configure() { // some work return this; }; function process() { // some work return this; } return { // public functions configure: configure, process: process }; }(jQuery);
(OTOH, if you wanted to create aliases to them, you would then have to bind them to the object) (OTOH,如果你想为它们创建别名,那么你必须将它们绑定到对象)
Or if configure and process are quite short, simple functions : 或者如果配置和处理很短,那么简单的功能:
filtersManager = (function ($) { return { // public functions configure: function () { // some work return this; }, process: function () { // some work return this; } }; }(jQuery));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.