Private variables and methods


I'm not currently stuck at any exercise, but I have a question. Do classes that inherit from other classes also inherit the private variables and methods? In other words, if I wanted to use a private variable outside the class, could I define a new class that inherited from the first one instead of creating a public method?

Replace this line with your code.

Up the Food-I-mean-Prototype Chain, something for the prototype chain

function Foo(bar) {
    var foobar = "private"; = bar;
    this.fooprivate = function() {
        return foobar;

newfoo = new Foo("bar");

console.log(;           // bar
console.log(newfoo.foobar);        // undefined
console.log(newfoo.fooprivate());  // private

Now let's create a subclass and see what it inherits.

function Bar(foo) { = foo;
Bar.prototype = new Foo();

newbar = new Bar("foo");

console.log(;           // foo
console.log(;           // undefined
console.log(newbar.foobar);        // undefined
console.log(newbar.fooprivate());  // private


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.