2 回答
![?](http://img1.sycdn.imooc.com/545862370001b03502200220-100-100.jpg)
TA贡献1875条经验 获得超5个赞
要链接功能,您需要返回this
或object
本身,您可以尝试这样的操作
这里
new bar()
将返回一个函数,foo()
将返回 obj,关于进一步
start
和end
方法调用,我们更新属性和返回参考对象本身
我将如何制作一个对象,我可以指示在某些时候做什么。见示例:
function bar(){
// call/execute the start() code
// do something
// do some other thing
// do something else
// call/execute the end() code
}
foo=new bar();
foo()
.start(function(param){
console.log("start");
})
.end(function(param){
console.log("end");
})
![?](http://img1.sycdn.imooc.com/533e4c5600017c5b02010200-100-100.jpg)
TA贡献1719条经验 获得超6个赞
您可以采用一些原型函数并返回一个隐式this的替代,一个带边界的函数this。
这些函数使用流畅的接口并返回this.
function Bar() {
return function() {
return this;
}.bind(this);
}
Bar.prototype.start = function (fn) { fn(); return this; };
Bar.prototype.end = function (fn) { fn(); return this; };
var foo = new Bar();
foo()
.start(function(param) {
console.log("start");
})
.end(function(param) {
console.log("end");
})
添加回答
举报