简体   繁体   English

如何在clojurescript中使用构造函数声明javascript对象

[英]How to declare javascript object with constructor in clojurescript

My code works with realm library and at some point it calls realm constructor: 我的代码与领域库一起使用,并在某些时候调用领域构造器:

(dependencies/realm. (clj->js options))

Realm was declared like this: Realm的声明如下:

(def realm (js/require "realm"))

Right now I want to temporarily mock realm object to not make calls to library. 现在,我想临时模拟领域对象,以不调用库。 I tried this approach: 我尝试了这种方法:

(def realm  #js {:schemaVersion (fn [])
                 :close         (fn [])})

It worked well for mocking close() and schemaVersion() functions but I'm getting error dependencies.realm is not a constructor . 它对于模拟close()和schemaVersion()函数效果很好,但是我遇到了错误dependencies.realm is not a constructor 。realm dependencies.realm is not a constructor

How can I add constructor declaration to realm object placeholder? 如何将构造函数声明添加到领域对象占位符?

Thanks. 谢谢。

In javascript a constructor is a function. 在javascript中,构造函数是一个函数。 Instead, you should have a function that returns an object: 相反,您应该具有一个返回对象的函数:

(def realm (fn [] #js {}))

I assume schemaVersion and close are static methods. 我假设schemaVersionclose是静态方法。 You can add them later with: 您可以稍后添加它们:

(goog.object/extend realm #js {:schemaVersion (fn [])
                               :close         (fn [])})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM