Coffeescript 在给定 class 的对象的情况下创建 class 的新实例
Coffeescript create new instance of a class given an object of that class
在 coffeescript 中,我有一个从 class 创建的对象。后来,我无法再访问那个 class,但我想创建一个新实例。例如:
# MySingleton.coffee
class MySingleton
module.exports = new MySingleton
然后:
# MySingletonTests.coffee
mySingleton = require './MySingleton'
# testSingleton = ???
# I would like to create a new MySingleton here
我想我想在这里使用 JS 的 Object.create 之类的东西,但是如果我调用 Object.create(mySingleton)
它似乎只是对旧对象的引用,而 mySingleton.prototype
是undefined
编辑:
我知道我可以在这个例子中导出 class,但我希望不要这样做,因为它在生产代码中是一个单例,我想确保它不会被更新,但在我的单元中测试我想制作它的新副本,这样状态就不会在测试之间持续存在
事实证明 new myInstance.constructor()
完全符合我的要求。
在 coffeescript 中,我有一个从 class 创建的对象。后来,我无法再访问那个 class,但我想创建一个新实例。例如:
# MySingleton.coffee
class MySingleton
module.exports = new MySingleton
然后:
# MySingletonTests.coffee
mySingleton = require './MySingleton'
# testSingleton = ???
# I would like to create a new MySingleton here
我想我想在这里使用 JS 的 Object.create 之类的东西,但是如果我调用 Object.create(mySingleton)
它似乎只是对旧对象的引用,而 mySingleton.prototype
是undefined
编辑: 我知道我可以在这个例子中导出 class,但我希望不要这样做,因为它在生产代码中是一个单例,我想确保它不会被更新,但在我的单元中测试我想制作它的新副本,这样状态就不会在测试之间持续存在
事实证明 new myInstance.constructor()
完全符合我的要求。