Private variables and methods


#1


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
#2

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

newfoo = new Foo("bar");

console.log(newfoo.bar);           // 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) {
    this.foo = foo;
}
Bar.prototype = new Foo();

newbar = new Bar("foo");

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

#3

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