Easy Namespacing (JS 1.8)


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

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`


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.