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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:35:50+00:00 2026-05-29T05:35:50+00:00

Is it possible in javascript to have a variable that is not able to

  • 0

Is it possible in javascript to have a variable that is not able to access out side the class’s functions, but is able to be accessed by classes that inherit it? I.E:

class1 has protected var x = 4;

class2 inherits class1;

class2.prototype.getVar = function(){return /* parent, uber, super, whatever */ this.x;};

var cl2 = new class2();

console.log(cl2.x) // undefined
console.log(cl2.getVar()) // 4
  • 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-29T05:35:51+00:00Added an answer on May 29, 2026 at 5:35 am

    No. Prototypal inheritance is limited to properties of objects.

    Variables within the constructor are only available to other code in that variable scope.

    You could probably come up with something like…

    function cls1() {
        var a = 'foo';
        this.some_func = function() {
            alert(a);
        };
    }
    
    function cls2() {
        cls1.apply(this, arguments);
        var cls1_func = this.some_func;
    
        var b = 'bar'
    
        this.some_func = function() {
            cls1_func.apply(this, arguments);
            alert(b);
        };
    }
    
    var x = new cls2;
    
    x.some_func();  // alert "foo"  alert "bar"
    

    Or to make it more specific to your pseudo code…

    function class1() {
        var x = 4;
        this.getVar = function() {
            return x;
        };
    }
    
    function class2() {
    
        class1.apply(this, arguments);
    
        var cls1_get_var = this.getVar;
    
        this.getVar = function() {
            return cls1_get_var.apply(this, arguments);
        };
    }
    
    class2.prototype = Object.create( class1.prototype );
    
    var cl2 = new class2;
    
    console.log(cl2.x) // undefined
    console.log(cl2.getVar()) // 4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to have XML-embedded JavaScript executed to assist in client-side (browser-based) XSL
I've read that JavaScript does not really have static variables(when i say static, I
I have a javascript variable containing the HTML source code of a page (not
I want to have horizontal lists that can run as wide as possible but
Possible Duplicate: Avoiding repeated constants in CSS I have a javascript file that I
Not sure this is possible but maybe you can help. I have a list,
Is it possible to have JavaScript compute a timestamp returned from PHP's time() function
Possible Duplicate: JavaScript Function Definition in ASP User Control Hi, I have a generic
Is it possible in javascript to convert some date in timestamp ? i have
Possible Duplicate: Escape string for use in Javascript regex I have a msg like

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.