javascript-如何“流利的API"; (也称为“链接")? [英] javascript - how to "fluent API" (also calling "chaining")?
本文介绍了javascript-如何“流利的API"; (也称为“链接")?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图理解的是如何在方法调用之后创建调用方法.
I am trying to understand is how to create call methods after a method call.
例如在jquery中,您有类似以下内容:
For example in jquery you have something like this:
$("blah").data("data-id");
我怎么做:
blah("cow").foo("moo");
笨蛋和foo在哪儿console.log(value)
?
推荐答案
您所指的是流利的API"(也称为链接").您的函数需要返回具有您要在其上调用的下一个方法的对象.例如,
What you're referring to is a "fluent API" (also calling "chaining"). Your functions need to return the object that has the next method you want to call on it. For example,
var obj = function(){
var self = this;
self.blah = function(v){ console.log(v); return self; };
self.foo = function(v){ console.log(v); return self; };
};
var o = new obj();
o.blah("cow").foo("moo");
See this article for more info: http://www.i-programmer.info/programming/javascript/4676-chaining-fluent-interfaces-in-javascript.html
这篇关于javascript-如何“流利的API"; (也称为“链接")?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文