[英]Coffeescript create new instance of a class given an object of that class
In coffeescript, I have an object created from a class. 在coffeescript中,我有一个从类创建的对象。 Later, I no longer have access to that class, but I would like to create a new instance. 后来,我不再有权访问该类,但是我想创建一个新实例。 For example: 例如:
# MySingleton.coffee
class MySingleton
module.exports = new MySingleton
and then: 接着:
# MySingletonTests.coffee
mySingleton = require './MySingleton'
# testSingleton = ???
# I would like to create a new MySingleton here
I think I would like to use something like JS's Object.create here, but if I call Object.create(mySingleton)
it seems to just make a reference to the old object, and mySingleton.prototype
is undefined
我想我想在这里使用JS的Object.create之类的东西,但是如果我调用Object.create(mySingleton)
它似乎只是对旧对象的引用,而mySingleton.prototype
是undefined
EDIT : I know I could just export the class in this example, but I was hoping not to because it is a singleton in production code and I would like to make sure it doesn't get new'd, but in my unit testing I would like to make new copies of it so state doesn't persist between tests 编辑 :我知道我可以在此示例中导出该类,但我希望不要这样做,因为它是生产代码中的一个单例,并且我想确保它不会被更新,但是在我的单元测试中想要为其制作新副本,因此在测试之间状态不会持久
事实证明, new myInstance.constructor()
确实可以完成我想做的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.