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

  • Home
  • SEARCH
  • 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 8211995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:36:59+00:00 2026-06-07T10:36:59+00:00

Is there a way to cross-reference an instance of a class or an instance

  • 0

Is there a way to cross-reference an instance of a class or an instance variable from different namespaces, taking into account that it does matter in which order are the script files defined in the main html application. Actually I want to know if there is any possibility to cross reference two different class instances, one pointing to a reference defined in a different namespace, and another variable defined in the second class pointing back to the first one.

Suppose i have a main.js file where i define a class which uses some instance variables defined in another namespace, let’s say in particle.js, where at the same time i define a variable pointing back to Main class public variable.

var Main = (function() {
    var p = new Particle();

    this.draw = true;
    this.width = 800;
    this.height = 600;

    function print() {
        console.log(p.width, ':', p.height);
    }

    return {
        draw : this.draw,
        width : this.width,
        height : this.height,
        print : print
    }
})();

function Particle() {

    this.width = Main.width;
    this.height = Main.height;
    this.print = function() {
        console.log(this.width, ':', this.height);
    }    
}


var p = new Particle();
p.print();
Main.print();

…and in the *.html the javascript files order would be:

<script src = 'main.js'></script>
<script src = 'particle.js'></script>

Actually this code is working as expected if you try it on firebug for example, but on the same logic on my real application, which is much complex, i got Main is undefined error in console. I know it’s possible to simulate real class modules using AMD with Require.js for example, but i don’t want to relay on AMD right now.

  • 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-07T10:37:01+00:00Added an answer on June 7, 2026 at 10:37 am

    I did not manage to get your code work on Chrome or Firefox, i always get an error on Main.width.

    The matter is that you refer to Main inside particle when you Main has not been fully constructed yet.

    There is no straightforward solution, the best I can think is delaying part of the initialisation of your Main singleton after you defined the Particle class.
    Alternately you can also reorder your code to respect dependencies.

    You have to remember that in javascript your code is evaluated when you call it.

    Here are my two proposals:

    Solution 1: Delay Main initialisation partly

    // Main.js --> loaded first
    var Main = new (function () {
        this.draw = true;
        this.width = 800;
        this.height = 600;
    
        // delayed initialization method
        this.init = function ()
        {
            var p = new Particle();
            this.print = function () {
                console.log(p.width, ':', p.height);
            }
        }
    })();
    
    //Particle.js --> loaded second
    function Particle() {
        this.width = Main.width;
        this.height = Main.height;
        this.print = function() {
            console.log(this.width, ':', this.height);
        }    
    }
    
    // call the delayed init method
    Main.init()
    
    var p = new Particle();
    p.print();
    Main.print();
    

    Solution 2: Split in 3 files to respect dependencies

    //Particle.js --> loaded first
    function Particle() {
        this.width = Main.width;
        this.height = Main.height;
        this.print = function() {
            console.log(this.width, ':', this.height);
        }    
    }
    
    // Main.js --> loaded in second position
    var Main = (function() {
        var p = new Particle();
        this.draw = true;
        this.width = 800;
        this.height = 600;
        function print() {
            console.log(p.width, ':', p.height);
        }
        return {
            draw : this.draw,
            width : this.width,
            height : this.height,
            print : print
        }
    })();
    
    // Init.js --> loaded third
    var p = new Particle();
    p.print();
    Main.print();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there way that I can read the file from remote server using fopen
Is there any way to create a pivot and/or cross-tab query using for SQLite
I've been thinking for a while if there's a way to get cross-domain AJAX
Is there way to get file from windows xp command prompt? I tried to
Is there way to copy a file into Plone with WebDAV and have Plone
is there way how to get name ov event from Lambda expression like with
Is there way to set @include mixin(); to variable? I tried this @mixin bg-gradient($fallback,
I have two enums that cross reference each other. Each one has a constructor
Is there a quick-reference guide to the iPhone SDK that's as fast and easy
Is there way to automatically maximize the output window on hitting build and then

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.