Easy Namespacing


/ Published in: JavaScript
Save to your folder(s)

Easy Namespacing
================
This version doesn't require JavaScript 1.8

Examples:

`"foo.bar.baz".namespace()` makes `foo.bar.baz`

`"foo::bar::baz".namespace("::")` makes `foo.bar.baz`

`namespace.call(obj, ns[, separator])` extends `ns` (split by separator or ".") onto `obj`


Copy this code and paste it in your HTML
  1. function namespace(fullNS, separator) {
  2. var parent = this;
  3. fullNS.split(separator || '.').forEach(function(ns) {
  4. parent = parent[ns] = parent[ns] || {}
  5. });
  6. }
  7.  
  8. String.prototype.namespace = function(separator, thisp) {
  9. namespace.call((thisp||window), this, separator);
  10. };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.