Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9003121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:40:22+00:00 2026-06-16T00:40:22+00:00

I’m trying to add a define module method to my top-level namespace, which first

  • 0

I’m trying to add a define module method to my top-level namespace, which first defines the namespace. For example,

APP.define('modules.moduleName', function () { 
    return {name: 'Module 1' }; 
});

But it seems that with my implementation of it, there is an assignment problem. Here is a simplified example of my implementation.

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 < 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'
    };
});

However, it seems namespace is not assigned under the APP namespace after assigning it to definition(). However var module = APP.define('module.example', function () { /*...*/ }) works. This also works:

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;
};

There must be something about object reference or callback scope I’m missing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:40:23+00:00Added an answer on June 16, 2026 at 12:40 am

    When you assign var namespace to APP.namespace( name ) you get last part of namespace by reference, but then that namespace = definition() just redefines your namespace variable to result of definition function.

    You can merge your APP.namespace and APP.define so your pre-last parent assigns last part of namespace as property to definition function

    The code is:

    var APP = APP || {};
    APP.define = function( nsString, definition) {
    
        var parts  = nsString.split('.'),
            parent = this,
            i;
    
        if (parts[0] === 'APP') {
            parts = parts.slice(1);
        }
    
        for ( i = 0; i < parts.length - 1; i++ ) {
            if (typeof parent[parts[i]] === 'undefined') {
                parent[parts[i]] = {};
            }
            parent = parent[parts[i]];
        }
    
        parent[parts.pop()] = definition();
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
We're building an app, our first using Rails 3, and we're having to build
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.