Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript Define Module Method Assignment
    primarykey
    data
    text
    <p>I'm trying to add a define module method to my top-level namespace, which first defines the namespace. For example,</p> <pre><code>APP.define('modules.moduleName', function () { return {name: 'Module 1' }; }); </code></pre> <p>But it seems that with my implementation of it, there is an assignment problem. Here is a simplified example of my implementation.</p> <pre><code>var APP = APP || {}; APP.namespace = function( nsString ) { var parts = nsString.split('.'), parent = this, i; if (parts[0] === 'APP') { parts = parts.slice(1); } for ( i = 0; i &lt; parts.length; i++ ) { if (typeof parent[parts[i]] === 'undefined') { parent[parts[i]] = {}; } parent = parent[parts[i]]; } return parent; }; // Simplified example of my define function APP.define = function( name, definition ) { var namespace = APP.namespace( name ); namespace = definition(); return namespace; }; // Example Module APP.define('modules.example', function () { return { name : 'Application', module : 'APP.module' }; }); </code></pre> <p>However, it seems namespace is not assigned under the APP namespace after assigning it to <code>definition()</code>. However <code>var module = APP.define('module.example', function () { /*...*/ })</code> works. This also works:</p> <pre><code>APP.define = function( name, definition ) { var namespace = APP.namespace( name ); var module = definition(); for (var prop in module) { if (module.hasOwnProperty[prop]) { namespace[prop] = module[prop]; } } return namespace; }; </code></pre> <p>There must be something about object reference or callback scope I'm missing.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload