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

后端 未结 2 1095
青春惊慌失措
青春惊慌失措 2020-12-24 04:02

Imagine the task is to create some utility lib in clojurescript so it can be used from JS.

For example, let\'s say I want to produce an equivalent of:



        
相关标签:
2条回答
  • 2020-12-24 04:23
    (defprotocol IFoo
      (bar [this x]))
    
    (deftype Foo [a b c]
      IFoo
      (bar [_ x]
        (+ a b c x)))
    
    (def afoo (Foo. 1 2 3))
    (bar afoo 3) ; >> 9
    

    Is the idiomatic way to do this.

    0 讨论(0)
  • 2020-12-24 04:28

    This was solved with JIRA CLJS-83 by adding a magic "Object" protocol to the deftype:

    (deftype Foo [a b c]
      Object
      (bar [this x] (+ a b c x)))
    (def afoo (Foo. 1 2 3))
    (.bar afoo 3) ; >> 9
    
    0 讨论(0)
提交回复
热议问题