假设任务是在ClojureScript中创建一些实用程序库,以便可以从JS中使用它。
例如,假设我想要生成等效的代码:
var Foo = function(a, b, c){
this.a = a;
this.b = b;
this.c = c;
}
Foo.prototype.bar = function(x){
return this.a + this.b + this.c + x;
}
var x = new Foo(1,2,3);
x.bar(3); // >> 9
我想到的一种实现方法是:
(deftype Foo [a b c])
(set! (.bar (.prototype Foo))
(fn [x]
(this-as this
(+ (.a this) (.b this) (.c this) x))))
(def x (Foo. 1 2 3))
(.bar x 3) ; >> 9
问题:在ClojureScript中是否有更优雅、更惯用的方式实现上述代码?