Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another namespacing pattern recommendation #24

Open
lauriro opened this issue Apr 12, 2012 · 1 comment
Open

Another namespacing pattern recommendation #24

lauriro opened this issue Apr 12, 2012 · 1 comment

Comments

@lauriro
Copy link

lauriro commented Apr 12, 2012

function ns(str, root) {
  return str.split(".").reduce(function(p, n){return p[n]=p[n]||{}}, root||this)
}

!function(self){
  self.x = 1
}(ns("myApp.util"))

!function(self){
  // dependencies
  var util = ns("myApp.util")

  self.x = util.x + 1
}(ns("myApp.other"))
@JamesMGreene
Copy link

I was about to suggest the same. My original Gist used Array#forEach but I've updated them to use Array#reduce after seeing @lauriro's much briefer implementation!

https://gist.github.com/3070677

Includes one that is basically the same as @lauriro's implementation, as well as one that actually enforces the root namespace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants