0

I have been a programmer for years now. Javascript seems to be the most quirky language I have came across so I am very stuck as to how I can perform this task.

My question is how can I override the constructor of a parent with prototype. For example:

This works:

function Superclass(arg1, arg2) {
alert(this.arg1);
alert(this.arg2);
}

function Subclass() {

}
Subclass.prototype = new Superclass();

//I am instituting the superclass as I do not know how to override the perant's method
var SubclassInstance = new Superclass("arg1", "arg2");

This won't work though:

function Superclass(arg1, arg2) {
alert(this.arg1);
alert(this.arg2);
}

function Subclass() {
//for this example I have not coded any behaviour for the subclass
}
Subclass.prototype = new Superclass();

//I am instituting the superclass as I do not know how to override the perant's method
var SubclassInstance = new Subclass("arg1", "arg2");

Thanks all

Geek Num 88
  • 5,264
  • 2
  • 22
  • 35
Oliver Scott-Brown
  • 57
  • 1
  • 1
  • 10
  • 1
    Could it be that your are confusing prototypal inheritance (a concept in JavaScript) with Prototype.js (a library)? – Felix Kling Mar 14 '14 at 21:37
  • possible duplicate of [how to call parent constructor?](http://stackoverflow.com/questions/6617780/how-to-call-parent-constructor) – Felix Kling Mar 14 '14 at 21:39
  • No confusion between Javascript and prototype. I was just using this as an example because of my lack of experience with JS and prototype. Thanks btw. I will check this out. – Oliver Scott-Brown Mar 14 '14 at 21:49
  • Either way, your example has nothing to do with Prototype.js. – Felix Kling Mar 14 '14 at 22:49

1 Answers1

0

To re use parent constructor you can do

      Parent.call(this,args)

What args can be and an explanation about constructor functions and their prototype can be found here: Prototypical inheritance - writing up

Community
  • 1
  • 1
HMR
  • 37,593
  • 24
  • 91
  • 160