2

I'm stuck with a problem using javascript...
I want to declare a private variable in a class that can't be used from its sublclass...what I've tried is:

function Person(){
    var _name
    this.setName = function(name){
        _name = name
    }
    this.getName = function(){
        return _name
    }
}

function GreetingPerson(){
    var self = this;
    self.sayHello = function(){
        console.log(self.getName() + ': "Hello!"');
    }
}

GreetingPerson.prototype = new Person()
GreetingPerson.prototype.contructor = GreetingPerson;

var manuel = new GreetingPerson()
manuel.setName('Manuel');
manuel.sayHello();

var world = new GreetingPerson()
world.setName('World');
world.sayHello();
manuel.sayHello();
console.log(manuel.name)

In this way the name variable is private, but it is also static, so the last wo sayHello method calls, will write the same output.
I've tried also changing the Person class in this way:

function Person(){
    this.setName = function(name){
        this.name = name
    }
    this.getName = function(){
        return this.name
    }
}

But in this way it is not longer private.
What is the correct way to achieve it?

2 Answers 2

2

EDIT: Using something like @teddybeard says, you can get it too:

function Person(){
    var _name;
    this.setName = function(name){
        _name = name;
    };
    this.getName = function(){
        return _name;
    };
  return this;
}

function GreetingPerson(){
    Person.call(this);
    this.sayHello = function(){
        console.log(this.getName() + ': "Hello!"');
    };
  return this;
}

GreetingPerson.prototype = new Person();
GreetingPerson.prototype.constructor = GreetingPerson;

var manuel = new GreetingPerson();
manuel.setName('Manuel');
manuel.sayHello();

var world = new GreetingPerson();
world.setName('World');
world.sayHello();
manuel.sayHello();
console.log(manuel._name);

But I'm not pretty sure if this is actually ok. The problem is that if you don't do something like Person.call(this); inside the constructor of GreetingPerson, you will not create a new instance of Person and it will always use the same _name value.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, it works. I noticed that in this way I don't need to do GreetingPerson.prototype = new Person() to make inheritance work
But if you remove that line, the variable manuel will not be an instance of Person, it will be just an instance of GreetingPerson. If you don't remove it, you should get an object that is an instance of both Person and GreetingPerson.
It also seems to work fine without the return this; parts. The Person.call(this); part seem to be the key here.
1

Check out Eloquent Javascript if you have time. I think this code should work for your purposes of inheritance.

function Person() {
    var _name
    this.setName = function(name) {
        _name = name
    }
    this.getName = function() {
        return _name
    }
}

function GreetingPerson() {
    Person.call(this);
    this.sayHello = function() {
        console.log(this.getName() + ': "Hello!"');
    }
}

// taken from Eloquent Javascript
function clone(object) {
    function OneShotConstructor() {}
    OneShotConstructor.prototype = object;
    return new OneShotConstructor();
}

GreetingPerson.prototype = clone(Person.prototype);
GreetingPerson.prototype.contructor = GreetingPerson;

var manuel = new GreetingPerson()
manuel.setName('Manuel');
manuel.sayHello();

var world = new GreetingPerson()
world.setName('World');
world.sayHello();
manuel.sayHello();
console.log(manuel.name)​;​

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.