0

How would one initiate a class from a variable in CoffeScript? in another words:

className = 'Domain'
domain = new className()

should.exist(domain)
'Domain'.should.equal(domain.constructor.name)

How would the line two should look like to satisfy the assertions? Thank You

Edit: The class is declared as following

class Domain
  constructor: (obj) ->
    for own key, value of obj
      @[key] = value

  save: (fn) ->
    self = @
 ...
module.exports = Domain

Peter Lyons answer works with a little adjustments to the way our class defined. In short, the solution:

DomainClass = require('./index')
newDomain = new DomainClass({...})
4

1 回答 1

2

您只需要在正确的范围内使用方括号查找类名。在浏览器中,new window[className]如果你的类在你可以做的模块中,你可以做或在节点中做new require('./models')[className]

于 2013-05-28T15:37:18.573 回答