1

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
4
  • Not directly; that's not how JavaScript is designed. Commented Feb 4, 2012 at 5:40
  • There's really no need for it. private and protected variables are designed only to keep programmers from harming themselves. Commented Feb 4, 2012 at 5:48
  • "there's really no need for it." Yes there is and you stated it - to keep programmers from harming themselves! Commented Apr 8, 2012 at 21:26
  • Proclass may be of some help. - Disclosure: I wrote it, so I'm biased ;) - You have to change the syntax a bit, but it's fairly usable. Commented Jul 28, 2013 at 4:16

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

2

I think you need to use a closure to achieve what your trying to do. Something like this:

            Class1 = function() {
                var x = 4;
                return {
                    getVar: function() {
                        return x;
                    }
                }
            } ();// executes the function immediately and returns an
                //an object with one method - getVar. Through closure this method
                //still has access to the variable x


            Class2 = function() { };// define a constructor function
            Class2.prototype = Class1;//have it inherit from Class1

            Cl2 = new Class2();//instantiate a new instance of Class2
            console.log(Cl2.x);//this is undefined
            console.log(Cl2.getVar());//this outputs 4

This is one of the neat things about javascript in that you can achieve the same things in javascript as you would in a class based language without all the extra key words. Douglas Crockford (always good to consult about javascript) explains prototypal inheritance here

Edit:

Just had a second look at your question.If you want newly created methods in your class to access the variable in the base class then you would have to call the getVar method within your own method.Like such:

              Class2 = function() {
                    this.getVar2 = function() {
                        return this.getVar();
                    }
                };


                   console.log(Cl2.getVar2()) //outputs 4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.