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 6075923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:32:55+00:00 2026-05-23T10:32:55+00:00

During instantiating an object VarA of ClassA I pass in a variable VarB which

  • 0

During instantiating an object VarA of ClassA I pass in a variable VarB which acts as a placeholder for an object of ClassB. After VarA is instantiated; somewhere later in the pipeline I assign newly instantiated object of ClassB TO VarB.

In ClassA I save reference to VarB & then use this to do any work.

The problem I am facing is that when I try to use any method on VarA that depends upon VarB, I get errors saying VarB is undefined. This confuses me because before I use these methods I make sure that VarB is properly instantiated. And since I am saving the reference to VarB, it should be pointing to the corect object, right?

UPDATE – Code sample as requested.

var varA, varB;

// Code

varA = new ClassA({
     varB: varB
});

// Code

varB = new ClassB({
   attributeB: "SOME_VALUE",
});

// Class Definition for ClassA which uses varB

var ClassA = Class.extend({

    varB: {},

    init: function(settings) {
        this.varB = settings.varB;
    },

    dummyMethod: function() {

       // Do Something with varB.attributeB

    }

});

Not sure if this matters but I am using John Resig’s Class Extend Util for Simple JavaScript Inheritance. as the base class of all my Classes.

  • 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-05-23T10:32:55+00:00Added an answer on May 23, 2026 at 10:32 am

    If you’ve got something like this:

    var VarB = null, VarA = new ClassA(VarB);
    

    and then later, when you’re ready, you do this:

    VarB = new ClassB();
    

    then what that “VarA” object is holding onto is the “null” value you passed in, and you’ve changed the value of “VarB” to something else. There’s no way to do what you describe, at least not exactly. JavaScript is strictly pass by value, which means that when you make a function call, you’re passing the value of the argument expression to the function. Inside the “ClassA” constructor, the fact that the “null” happened to come from a variable in the calling scope called “VarB” is completely hidden; it’s just a “null”.

    Now, what you can do is this:

    var VarB = { actualB: null }, VarA = new ClassA(VarB);
    

    and then later:

    VarB.actualB = new ClassB();
    

    In that case, you would not be changing the value of “VarB”; you’d be changing the value of a property of the object referenced by “VarB”. If the “ClassA” code were written to stash that parameter and refer to its “actualB” property, then it would work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

During my apprenticeship, I have used NHibernate for some smaller projects which I mostly
During software development, there may be bugs in the codebase which are known issues.
Not during instantiation, but once instantiation of singleton object is done, what will happen
I'm having a problem whereby a loop which is instantiating two objects, which in
I'm instantiating and scheduling a timer variable but Xcode compiler and analyzer marks my
I encountered this during object-relational mapping while creating a row mapper (to map a
During a discussion about security, a developer on my team asked if there was
During a typical day programming, I implement functions in a way that I would
During a complicated update I might prefer to display all the changes at once.
During an ASP.NET page load I'm opening and closing multiple System.Data.SqlClient.SqlConnections inside multiple controls

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.