Not unless you make all variables "public", i.e. make them members of the Function
either directly or through the prototype
property.
var C = function( ) {
this.x = 10 , this.y = 20 ;
this.modify = function( ) {
this.x = 30 , this.y = 40 ;
console.log("(!) C >> " + (this.x + this.y) ) ;
} ;
} ;
var A = function( ) {
this.modify = function( ) {
this.x = 300 , this.y = 400 ;
console.log("(!) A >> " + (this.x + this.y) ) ;
} ;
} ;
A.prototype = new C ;
var B = function( ) {
this.modify = function( ) {
this.x = 3000 , this.y = 4000 ;
console.log("(!) B >> " + (this.x + this.y) ) ;
} ;
} ;
new C( ).modify( ) ;
new A( ).modify( ) ;
new B( ).modify( ) ;
You will notice a few changes.
Most importantly the call to the supposed "super-classes" constructor is now implicit within this line:
<name>.prototype = new C ;
Both A
and B
will now have individually modifiable members x
and y
which would not be the case if we would have written ... = C
instead.
Then, x
, y
and modify
are all "public" members so that assigning a different Function
to them
<name>.prototype.modify = function( ) { /* ... */ }
will "override" the original Function
by that name.
Lastly, the call to modify
cannot be done in the Function
declaration because the implicit call to the "super-class" would then be executed again when we set the supposed "super-class" to the prototype
property of the supposed "sub-classes".
But well, this is more or less how you would do this kind of thing in JavaScript.
HTH,
FK