Search code examples
objectcoffeescriptglobalself-reference

Self referencing in global, static object in CoffeeScript


Is there any way to accomplish self referencing as below within a global, static object in CS?

@Obj
  x: ['string']
  y: [@x]

From what I've read, this doesn't seem possible. Mainly due to the object having not been created yet at time of assignment of y. Some form of getter and setter (e.g. __definedSetter__, __defineGetter__, etc.) seem like the most likely options but I would have to turn this object into a class and there are a whole slew of issues with that in CS and I have little reason to do this aside from this self referencing issue. Is there any workaround?

Thanks!


Solution

  • As you mentioned, this would be possible with a class (you can reference and assign Obj.x to Obj.y in the constructor). If it's a static object, I'd just assign it in a separate line with @Obj.y = [@Obj.x].