javascript namespace definition self reference

WindowsMaker

I am trying to define a javascript namespace, but within the namespace i have objects that are extensions of basic classes (using backbone and underscore):

namespace = {
    subspace: {
        A: some_class.extend({...}),
        B: A.extend({...})
    }
}

but this creates a problem because A is not yet defined. What is the best practice in this situation? furthermore, i have multiple subspaces in this namespace and subsequent ones cannot reference the previous one.

namespace = {
    subspace: {
        A: some_class.extend({...}),
        B: A.extend({...})
    },
    subspace2: {
        some_function: function(){
            <how do i create an instance of A for example? > 
        }
    }
}
Evgeniy

You should use helper function to manage it. Here is an implementation from "JavaScript Patterns" by Stoyan Stefanov.

Consider application global object as MYAPP.

and function of adding namespaces:

MYAPP.namespace('MYAPP.modules.module1')

and here is example implementation of namespace method:

MYAPP.namespace = function(ns_string) {
    var parts = ns_string.split('.'),
        parent = MYAPP,
        i;

    if (parts[0] === 'MYAPP') {
        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;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to reference self in a definition?

From Dev

Javascript prototype chain self reference

From Dev

Is self reference of a variable a bad thing in JavaScript?

From Dev

Can I provide a prototype definition of me/that/self = this in javascript?

From Dev

Can I provide a prototype definition of me/that/self = this in javascript?

From Dev

Path definition with namespace

From Dev

Class definition inside namespace

From Dev

namespace definition changes mimetype

From Dev

Multiple definition of namespace function

From Dev

UIWebView JavaScript losing reference to iOS JSContext namespace (object)

From Dev

Which is considered good practice in Javascript: using this or self reference?

From Dev

Function definition precedes declaration in namespace

From Dev

C++ Namespace Declaration and Definition

From Dev

Typescript definition for a class within a namespace

From Dev

JAXB issue with missing namespace definition

From Dev

What is definition of reference type?

From Dev

Using "self" in function arguments definition

From Dev

Ambiguous reference to namespace within an inline namespace

From Dev

Return reference with lifetime of self

From Dev

jQuery selector reference to self

From Dev

How to self reference in XSD

From Dev

Symfony doctrine self reference

From Dev

Java Self Reference with Inhertiance

From Dev

Function Self Reference

From Dev

Database Design For Self Reference

From Dev

Unresolved reference for 'self'

From Dev

static class self reference

From Dev

Java Self Reference with Inhertiance

From Dev

Return reference with lifetime of self