2

After looking through some of the other threads inheritance looks a bit complicated. the why does the getAge() function not return the this._age value in Option 1? and what is best practice between Option 1 and Option 2?


Option 1

function Person(){
    this._age = 21;
}

Person.prototype.getAge(){
    return this._age;//undefined
}

var person = new Person();
person.getAge();

Option 2

function Person(){
    var _age = 21,
    getAge:function(){
        return _age;
    }
}

Finally what would be the different be between the above two and the following?


var person = new Person();
person.age = 21;

I just might be confusing myself.

Sully
  • 14,672
  • 5
  • 54
  • 79
nontechguy
  • 751
  • 5
  • 21
  • In the `Option 1`, line 5 should throw an error. Try `Person.prototype.getAge = function() {`. In the `Option 2` is again another syntax error. – 0101 Jun 16 '14 at 12:39
  • Option 2 will not define any property of the new instance, only local variables within the function. – Maurice Perry Jun 16 '14 at 12:48

3 Answers3

0

Try this code:

var Person = function(){
    this._age = 21;
}

Person.prototype.getAge = function(){
    return this._age;
}

var person = new Person();
alert(person.getAge());

Option 2:

var Person = function(){
    this._age = 21;
    this.getAge=function(){
        return this._age;
    }
}
var person = new Person();
alert(person.getAge());
Sully
  • 14,672
  • 5
  • 54
  • 79
Valijon
  • 12,667
  • 4
  • 34
  • 67
0

There's no real "best practice" to speak of. The issue in your first example is a simple syntax error:

function Person(){
    this._age = 21;
}

Person.prototype.getAge = function(){
    return this._age; //21
}

var person = new Person();
person.getAge();

Option 2 also has a lot of syntax errors. Too many to go into detail here. This is the best explanation of these concepts that I know of: http://javascript.crockford.com/private.html

In option 1 and your third example, external code can modify the value of _age and age respectively. Which is to say that in option 1 I can come along and go:

person._age = 1000;

and that will actually update the value of _age within the object. In the case of what you're trying to do with option 2, no functions that are declared outside the Person "class" (really an Object) can modify the value of _age, which is probably a good thing in this context.

Sully
  • 14,672
  • 5
  • 54
  • 79
Dan Cowell
  • 385
  • 1
  • 4
  • Thanks you were right about the syntax error, although that was just a typo on my side and not replicated in my code. Your answer did help me though as I have identified where the problem is. I am using the createjs library and it is an issue with the parent container. I have not sorted it out but I am half a step closer. – nontechguy Jun 16 '14 at 16:44
  • I am using createjs and so I have added on to this question. You can find the new post here if you are interested.http://stackoverflow.com/questions/24257896/containers-and-inheritance – nontechguy Jun 17 '14 at 07:22
0

Inheritance can be easy:

inheritance using underscore

or if you like ExtJS

ExtJs inheritance behaviour

Community
  • 1
  • 1
Upperstage
  • 3,747
  • 8
  • 44
  • 67