/ 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`
================
*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`
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function namespace(fullNS, separator) void let(parent = this) fullNS.split(separator || '.').forEach(function(ns) parent = parent[ns] = parent[ns] || {} ); String.prototype.namespace = function(separator, thisp) void namespace.call((thisp||window), this, separator);