B
burningodzilla
Hi all - I'm preparing to dive in to more complex application
development using javascript, and among other things, I'm having a hard
time wrapping my head around an issues regarding "inheritance" using
the prototype property. I realize there are no classes in JS, that code
therefore lives in objects instead of class definitions, and that
"inheritance" must be achieved prototypically and not classically.
That said, on with the code. Say I have an object I wish to use as a
super type:
function SuperA(name) {
this.name = name;
this.alertName = function() { alert(this.name); }
}
It takes a string in the constructor, assigns it to the name property,
and provides a way to display that name in an alert box, via alertName
method. What I've come to learn is that if I want to create a sub type,
I must assign a new instance of the super type to the prototype of the
sub type. Like so:
function SubA(name) {
//sub type properties
}
SubA.prototype = new SuperA();
This works fine in that when I create an instance of SubA, it
effectively inherits everything from SuperA. What I don't understand is
why this doesn't achieve the same effect:
function SubA(name) {
this.prototype = new SuperA(name);
}
This simply assigns an instance of the super type to the prototype of
the sub type instance in construction, yet if I create an instance and
attempt to call the method:
var supa = new SubA('one');
supa.alertName();
I get a JS error stating that supa.alertName is not a function. Note
that if I augment the the latter constructor to show the memebers in
construction:
function SubA(name) {
this.prototype = new SuperA(name);
for(var m in this.prototype) {
alert(m + ": " + typeof(this.prototype[m]) + ": " +
this.prototype[m]);
}
}
I get two alerts; the first being:
name: string: one
and the second:
alertName: function: function() { alert(this.name); }
I'd appreciate any clarity that is offered. Thanks!
development using javascript, and among other things, I'm having a hard
time wrapping my head around an issues regarding "inheritance" using
the prototype property. I realize there are no classes in JS, that code
therefore lives in objects instead of class definitions, and that
"inheritance" must be achieved prototypically and not classically.
That said, on with the code. Say I have an object I wish to use as a
super type:
function SuperA(name) {
this.name = name;
this.alertName = function() { alert(this.name); }
}
It takes a string in the constructor, assigns it to the name property,
and provides a way to display that name in an alert box, via alertName
method. What I've come to learn is that if I want to create a sub type,
I must assign a new instance of the super type to the prototype of the
sub type. Like so:
function SubA(name) {
//sub type properties
}
SubA.prototype = new SuperA();
This works fine in that when I create an instance of SubA, it
effectively inherits everything from SuperA. What I don't understand is
why this doesn't achieve the same effect:
function SubA(name) {
this.prototype = new SuperA(name);
}
This simply assigns an instance of the super type to the prototype of
the sub type instance in construction, yet if I create an instance and
attempt to call the method:
var supa = new SubA('one');
supa.alertName();
I get a JS error stating that supa.alertName is not a function. Note
that if I augment the the latter constructor to show the memebers in
construction:
function SubA(name) {
this.prototype = new SuperA(name);
for(var m in this.prototype) {
alert(m + ": " + typeof(this.prototype[m]) + ": " +
this.prototype[m]);
}
}
I get two alerts; the first being:
name: string: one
and the second:
alertName: function: function() { alert(this.name); }
I'd appreciate any clarity that is offered. Thanks!