Posted By

Sephr on 02/23/09


Tagged

namespacing js18


Versions (?)

Easy Namespacing (JS 1.8)


 / Published in: JavaScript
 

Easy Namespacing

Requires 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

  1. function namespace(fullNS, separator) void
  2. let(parent = this) fullNS.split(separator || '.').forEach(function(ns)
  3. parent = parent[ns] = parent[ns] || {}
  4. );
  5.  
  6. String.prototype.namespace = function(separator, thisp) void
  7. namespace.call((thisp||window), this, separator);

Report this snippet  

You need to login to post a comment.