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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:58:47+00:00 2026-06-04T15:58:47+00:00

I have a class within Javascript with a variable assigned in the constructor. I

  • 0

I have a “class” within Javascript with a variable assigned in the constructor. I also use prototype to store variables/methods:

var MyClass = function() 
{ 
    this.age = 100;
};

MyClass.prototype.name = "John";
MyClass.prototype.getAge = function() { return this.age};

alert(MyClass.prototype.name); //Alerts John
alert(MyClass.age); //undefined as expected
alert(MyClass.prototype.getAge()); //undefined??

So from what I can tell, there is no way I can access the constructor variables that are stored within MyClass unless I create an object from the Class:

var theClass = new MyClass();
alert(theClass.age);
alert(theClass.getAge());​

Both alerts will return the age correctly.

So to reiterate, can I access the constructor variables directly from the MyClass without needing to create a variable?

  • 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-04T15:58:48+00:00Added an answer on June 4, 2026 at 3:58 pm

    No. The variable age in your example is created when the constructor function runs; therefore it’s not going to be available until you run the function.

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

Sidebar

Related Questions

If I have a class called myclass does having static methods within the class
Say I have a class which represents a person, a variable within that class
I have a Java class for use with JUnit 4.x. Within each @Test method
I want to use javascript to check if a class exists within a UIWebView.
I have a jquery class within a normal class in javascript. Is it possible
I have a template class ( Node is an inner class within a BST).
I want to hide all elements within a div which have class name starting
I have a class library that has settings within an app.config. I know that
I have a generic class that takes a type T . Within this class
I have a property within a class, that I need to iterate through all

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.