ClojureScript中的Fluent接口
我正在用ClojureScript编写一个库,它将公开一个公共JavaScript API。由于它必须模仿现有JavaScript库的API,因此我想介绍相同类型的fluent API:ClojureScript中的Fluent接口,clojure,clojurescript,Clojure,Clojurescript,我正在用ClojureScript编写一个库,它将公开一个公共JavaScript API。由于它必须模仿现有JavaScript库的API,因此我想介绍相同类型的fluent API: myLib.prepareToDo() .something() .and('also') .somethingElse() .run(function(err, result) { console.log("yay!"); }); 在Javascri
myLib.prepareToDo()
.something()
.and('also')
.somethingElse()
.run(function(err, result) {
console.log("yay!");
});
在Javascript中,可以创建如下流畅的API):
我可以这样称呼它:
var obj = new MyClass(5);
obj.foo(1).foo(2).foo(3);
现在,据我所知,ClojureScript中没有这个
的概念,尽管很显然可以将它作为访问
我不知道如何使用它,因此我的问题
如何在ClojureScript中创建一个流畅的界面?(defrecord)
,并进行救援。将“magic”协议Object
扩展到我们的记录或类型会导致定义的方法在JavaScript对象中显示为成员函数。为了启用“fluent接口”,一些方法返回MyClass
的实例
(defrecord MyClass [a b]
Object
(something [this] this)
(and-then [this s] (assoc this :a s))
(something-else [this] (assoc this :b (str a "-" a)))
(run [this f] (f a b)))
然后我们可以有一个JavaScript客户端,如下所示:
var myClass = new my_namespace.core.MyClass();
myClass.something()
.and_then("bar")
.something_else()
.run(function(a, b) {
console.log(a + " - " + b) });
(defrecord)
和救援。将“magic”协议Object
扩展到我们的记录或类型会导致定义的方法在JavaScript对象中显示为成员函数。为了启用“fluent接口”,一些方法返回MyClass
的实例
(defrecord MyClass [a b]
Object
(something [this] this)
(and-then [this s] (assoc this :a s))
(something-else [this] (assoc this :b (str a "-" a)))
(run [this f] (f a b)))
然后我们可以有一个JavaScript客户端,如下所示:
var myClass = new my_namespace.core.MyClass();
myClass.something()
.and_then("bar")
.something_else()
.run(function(a, b) {
console.log(a + " - " + b) });
(defrecord)
和救援。将“magic”协议Object
扩展到我们的记录或类型会导致定义的方法在JavaScript对象中显示为成员函数。为了启用“fluent接口”,一些方法返回MyClass
的实例
(defrecord MyClass [a b]
Object
(something [this] this)
(and-then [this s] (assoc this :a s))
(something-else [this] (assoc this :b (str a "-" a)))
(run [this f] (f a b)))
然后我们可以有一个JavaScript客户端,如下所示:
var myClass = new my_namespace.core.MyClass();
myClass.something()
.and_then("bar")
.something_else()
.run(function(a, b) {
console.log(a + " - " + b) });
(defrecord)
和救援。将“magic”协议Object
扩展到我们的记录或类型会导致定义的方法在JavaScript对象中显示为成员函数。为了启用“fluent接口”,一些方法返回MyClass
的实例
(defrecord MyClass [a b]
Object
(something [this] this)
(and-then [this s] (assoc this :a s))
(something-else [this] (assoc this :b (str a "-" a)))
(run [this f] (f a b)))
然后我们可以有一个JavaScript客户端,如下所示:
var myClass = new my_namespace.core.MyClass();
myClass.something()
.and_then("bar")
.something_else()
.run(function(a, b) {
console.log(a + " - " + b) });