Evan Hahn
Evan Hahn

Reputation: 12712

Is there a more CoffeeScripty way to do this?

Here's some code I have:

window.MyNamespace ?= {}
window.MyNamespace.Subnamespace ?= {}
window.MyNamespace.Subnamespace.Subsubnamespace ?= {}

Is there a way to clean this up in a CoffeeScripty way?

Upvotes: 1

Views: 16

Answers (1)

mutil
mutil

Reputation: 3305

The only alternative I can think of, is to use this somewhat obscure syntax:
((window.MyNamespace ?= {}).Subnamespace ?= {}).Subsubnamespace ?= {}

See also this issue which proposes a way to create an object property if it doesn't exist.

Upvotes: 2

Related Questions