javascript - How do I create an JS Object with methods and constructor in ClojureScript -



javascript - How do I create an JS Object with methods and constructor in ClojureScript -

imagine task create utility lib in clojurescript can used js.

for example, let's want produce equivalent of:

var foo = function(a, b, c){ this.a = a; this.b = b; this.c = c; } foo.prototype.bar = function(x){ homecoming this.a + this.b + this.c + x; } var x = new foo(1,2,3); x.bar(3); // >> 9

one way accomplish came is:

(deftype foo [a b c]) (set! (.bar (.prototype foo)) (fn [x] (this-as (+ (.a this) (.b this) (.c this) x)))) (def x (foo. 1 2 3)) (.bar x 3) ; >> 9

question: there more elegant/idiomatic way of above in clojurescript?

this solved jira cljs-83 adding magic "object" protocol deftype:

(deftype foo [a b c] object (bar [this x] (+ b c x))) (def afoo (foo. 1 2 3)) (.bar afoo 3) ; >> 9

javascript clojure interop clojurescript

Comments

Popular posts from this blog

delphi - blogger via idHTTP : error 400 bad request -

c++ - cuda, pycuda -- how to write complex numbers -- errors:class "cuComplex" has no member "i" -

How do I check if an insert was successful with MySQLdb in Python? -