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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:53:39+00:00 2026-06-07T01:53:39+00:00

I wanted to know how can we read the value of a local variable

  • 0

I wanted to know how can we read the value of a local variable in another class / instance of a class in javascript.

eg:

I have a method in a class :

myClass1.prototype.myMethod0 = function()

{
 this._myVar = null; //initialize this._myVar
}

myClass1.prototype.myMethod1 = function(list)
{
 this._myVar = msg.list;
}

and

myClass1.prototype.myMethod2 = function()
{
 //do something 
// and update the list like say:

list1 = this._myVar; //access the this_myVar.

}

and in my another calss say,

myClass2.prototype.myMethod = function()
{
 //call the class1's method here..

myClass1.prototype.myMethod2();

}

myMethod2 is a callback and i bind it in myClass2.

It means, actually, myMethod2 is being called like this: myClass1.callback();

But my problem is , when i call myClass1.prototype.myMethod2();, the list1 = this._myVar; is not getting updated and it is becoming undefined. I am not getting the fix for the same.

The problem is the variable, “this._myVar” is “undefined” in myMethod2 of myclass1

  • 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-07T01:53:42+00:00Added an answer on June 7, 2026 at 1:53 am

    What do is possible but not the way you did it:

    1. Without the var keyword, a variable is not local but global. So your code actually created a global variable this_myVar which is shared by all JavaScript code on the same page.

    2. list1 will be undefined if myMethod1() was never called.

    See [here for an introduction into JavaScript OO programming][1].

    [EDIT] I see you updated your question and the variable is now accessed via this. The automatic variable this is only assigned when the function is called as a “method”.

    myClass1.prototype.myMethod2() will call the original definition and this will be undefined.

    You have to use code like this:

    var inst = new myClass1();
    inst.myMethod2();
    

    Inside of myMethod2, the variable this will have the same value as inst if you call it like that.

    [EDIT2] If you want to access instance properties, then you must call myMethod2() via an instance of myClass1 – JavaScript doesn’t try to read your mind what your code might mean.

    So at the time when you want to call any method of myClass1, you must have an instance of it. Try to create one in myClass2:

    var myClass2 = function() {
        this.inst1 = new myClass1();
    }
    
    myClass2.prototype.myMethod = function() {
        this.inst1.myMethod2();
    }
    

    Alternatively, you will have to pass the instance to the method:

    myClass2.prototype.myMethod = function(inst1) {
        inst1.myMethod2();
    }
    

    But if you call myMethod2() like any other global method, this won’t have any useful value. There is way to say “When I call a function with a prefix of myClass1.prototype., then look for an instance of myClass1 and put that into this“. What should JavaScript do when there are 15 instances of this type? Select one by random?

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

Sidebar

Related Questions

I wanted to know if we can have a unique identifier for all objects
I wanted to know if anybody can explain me the following piece of Javascript
I just wanted to know if I can point to class using this implementation:
I have read that the Facebook sharer is deprecated, So I wanted to know
I wanted to know how can we get the index of the array if
Thanks for looking at this question. I wanted to know how can I use
Hello I am new to MATLAB , I wanted to know how can I
To make a poll form using an applet, i wanted to know how can
I wanted to know that how can we find how much time android device
I just started to work with Silverlight and wanted to know how I can

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.