【发布时间】:2017-09-30 14:49:00
【问题描述】:
我一直在寻找一种以直接方式扩展函数的方法,例如 C# extension method。
我已经尝试了在这篇文章Method Chaining in JavaScript 中找到的以下语句,并且效果很好。
var FOO = function(){
this.whateverFunc = function(){
console.log("whatever func");
}
};
FOO.prototype.first = function(){
console.log("first func");
return this;
};
FOO.prototype.second = function(){
console.log("second func");
return this;
};
然后我可以链接它:
var foo = new FOO();
foo.first().second();
//Output
//first func
//second func
但是:我的项目具有以下“模式”:
var FOO = (function(){
var foo{
firstFunc: function(){
//implement
},
secondFunc: function(){
//implement
},
}
return foo;
}());
即使我不使用 IIFE,它也不起作用。
var FOO = function() { var foo{}; return foo; };
有没有办法在该模式中使用链接功能?我怎么能做到这一点(如果可能的话!):
FOO.first().second();
谢谢。
【问题讨论】:
标签: javascript c# jquery function chaining