{"id":6321,"date":"2014-04-15T19:46:14","date_gmt":"2014-04-15T19:46:14","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/15\/uncaught-referenceerror-namespace-is-not-defined-when-namespacing-in-coffeescript-collection-of-common-programming-errors\/"},"modified":"2014-04-15T19:46:14","modified_gmt":"2014-04-15T19:46:14","slug":"uncaught-referenceerror-namespace-is-not-defined-when-namespacing-in-coffeescript-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/15\/uncaught-referenceerror-namespace-is-not-defined-when-namespacing-in-coffeescript-collection-of-common-programming-errors\/","title":{"rendered":"Uncaught ReferenceError: namespace is not defined when namespacing in coffeescript-Collection of common programming errors"},"content":{"rendered":"<p>The <code>namespace<\/code> function from that question:<\/p>\n<pre><code>namespace = (target, name, block) -&gt;\n  [target, name, block] = [(if typeof exports isnt 'undefined' then exports else window), arguments...] if arguments.length &lt; 3\n  top    = target\n  target = target[item] or= {} for item in name.split '.'\n  block target, top\n<\/code><\/pre>\n<p>isn&#8217;t part of CoffeeScript, you have to define that helper yourself. Presumably you don&#8217;t want to repeat it in every file so you&#8217;d have a <code>namespace.coffee<\/code> file (or <code>util.coffee<\/code> or &#8230;) that contains the <code>namespace<\/code> definition. But then you&#8217;re left with the problem of getting your <code>namespace<\/code> function into the global namespace. You could do it by hand:<\/p>\n<pre><code>namespace = (target, name, block) -&gt;\n  [target, name, block] = [(if typeof exports isnt 'undefined' then exports else window), arguments...] if arguments.length &lt; 3\n  top    = target\n  target = target[item] or= {} for item in name.split '.'\n  block target, top\n\n(exports ? @).namespace = namespace\n# or just (exports ? @).namespace = (target, name, block) -&gt; #...\n<\/code><\/pre>\n<p>Demo: http:\/\/jsfiddle.net\/ambiguous\/Uv646\/<\/p>\n<p>Or you could get funky and use <code>namespace<\/code> to put itself into the global scope:<\/p>\n<pre><code>namespace = (target, name, block) -&gt; #...\nnamespace '', (exports, root) -&gt; root.namespace = namespace\n<\/code><\/pre>\n<p>Demo: http:\/\/jsfiddle.net\/ambiguous\/3dkXa\/<\/p>\n<p>Once you&#8217;ve done one of those, your <code>namespace<\/code> function should be available everywhere.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The namespace function from that question: namespace = (target, name, block) -&gt; [target, name, block] = [(if typeof exports isnt &#8216;undefined&#8217; then exports else window), arguments&#8230;] if arguments.length &lt; 3 top = target target = target[item] or= {} for item in name.split &#8216;.&#8217; block target, top isn&#8217;t part of CoffeeScript, you have to define that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6321","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6321","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6321"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6321\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}