Family = function(name) {
this._Name = name;
}
Family.prototype = {
getName: function() {
return this._Name;
},
People: function(num) {
this._Number = num;
}
}
Family.People.prototype = {
clearNumber: function() {
this._Number = 0;
}
}
People is a nested class. Its parent class is Family.
I get the error that Family.People is undefined. Could someone correct the code above?
Working code
This will work. But you have to be aware, that when you call:
f.Peopleis just an object constructor, and not an instance of some other object. You will have to instantiate it as well like:Sou you have a constructor within your instance which is rather confusing.
A better approach
So it would probably be better if you’d write your prototypes this way:
This actually makes a class within a class (and not within instances). So upper lines would be called this way:
Drill down of
finstance would look like:Private variables
This way we make variables private and only accessible within so they can’t be manipulated outside. You must always use functions to manipulate them. This makes it more robust especially when there are certain business rules related to variable values.
Drill down of
finstance would now look more like a class object instance: