I am studying the accessibility to variables from methods inherited from a parent to a child object.
The inheritance is performed associating an instance of the parent to the prototype of the child's constructor:
Function.prototype.extend = function(child) {
var prototype = new this;
child.prototype = prototype;
return child;
};
So I define a parent class:
var Parent = function() {
var self = this;
this.init = function() {
self.name = 'Abram';
};
this.getName = function() {
alert('self.name = ' + self.name);
};
this.init();
};
and a child class:
var Baby = Parent.extend(function(name) {
var self = this;
this.init = function() {
self.name = name;
};
this.init();
});
// instantiate a child object:
var baby = new Baby('Jimmy');
// call the inherited method:
baby.getName(); // undefined!
The method getName is correctly inherited, but it has no access to the self.name member.
How come, if it is available in the scope of the child class instance?
Checking the body of the method with alert(baby.getName) I get:
function () {
alert("self.name = " + self.name);
}
which is exactly what I expected.
But why it does not resolve the self variable locally?