Hi everyone,
I have the following code using inheritence The important part is the
function getName(). The result should alert 'John Doe':
function First(name) {
this.name = name;
this.getName = function() {
return this.name;
}
}
function Doe(firstName) {
this.base = First;
this.base(firsN ame);
this.getName = function() {
return this.base.getNa me()+' Doe';
}
}
d = new Doe('John');
alert(d.getName ());
But it doesn't work, because it says this.base.getNa me() is not a function.
That could be, but how can I still accompish this? There is no
this.super or something as far as I know. Is there a nice way of
doing this. I figured I could do the following in Doe:
this.getParentN ame = this.getName;
this.getName = function() {
return this.getParentN ame()+' Doe';
}
But thats not very neat because when I inhert from this object and I
apply the same trick to that new object then I'll create neverending
recursion, finally causing a stackoverflow. Anyone know any other way?
Thanks in advance,
Vincent
I have the following code using inheritence The important part is the
function getName(). The result should alert 'John Doe':
function First(name) {
this.name = name;
this.getName = function() {
return this.name;
}
}
function Doe(firstName) {
this.base = First;
this.base(firsN ame);
this.getName = function() {
return this.base.getNa me()+' Doe';
}
}
d = new Doe('John');
alert(d.getName ());
But it doesn't work, because it says this.base.getNa me() is not a function.
That could be, but how can I still accompish this? There is no
this.super or something as far as I know. Is there a nice way of
doing this. I figured I could do the following in Doe:
this.getParentN ame = this.getName;
this.getName = function() {
return this.getParentN ame()+' Doe';
}
But thats not very neat because when I inhert from this object and I
apply the same trick to that new object then I'll create neverending
recursion, finally causing a stackoverflow. Anyone know any other way?
Thanks in advance,
Vincent
Comment