Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Call An Inherited JavaScript Constructor With Parameters?

Tags:

Greetings,

After reading the following article I have a question: https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript

In the inheritance example, the Person constructor doesn't take any parameters. How would this same example look if I were to add one and call it from the Student constructor?

Thanks!

like image 346
kgarske Avatar asked Aug 30 '10 15:08

kgarske


People also ask

How do you call a constructor in JavaScript?

Constructors can be invoked only using the new keyword and the new keyword can be used only to invoke constructors. In javascript, the situation is unfortunately not so strict. So, calling function not designed as constructor will not result in an error.

Are constructors inherited in JavaScript?

Conclusion. The constructor property is a piece of the inheritance mechanism in JavaScript.

How do we invoke a constructor function?

The this keyword in Java is a reference to the object of the current class. Using it, you can refer a field, method or, constructor of a class. Therefore, if you need to invoke a constructor explicitly you can do so, using "this()".

How does a constructor work in JavaScript?

A constructor is a special function that creates and initializes an object instance of a class. In JavaScript, a constructor gets called when an object is created using the new keyword. The purpose of a constructor is to create a new object and set values for any existing object properties.


2 Answers

Well, a way that you can re-use the logic of the Person constructor is invoking it with call or apply, for example:

function Person(gender) {
  this.gender = gender;
}

function Student(gender) {
  Person.apply(this, arguments);
}
Student.prototype = new Person(); // make Student inherit from a Person object
Student.prototype.constructor = Student; // fix constructor property

var foo = new Student('male');
foo.gender;             // "male"
foo instanceof Student; // true
foo instanceof Person;  // true

If you want to prevent the execution of the Person constructor when is called without arguments (like in the line: Student.prototype = new Person();), you can detect it, e.g.:

function Person(gender) {
  if (arguments.length == 0) return; // don't do anything
  this.gender = gender;
}
like image 197
Christian C. Salvadó Avatar answered Sep 19 '22 05:09

Christian C. Salvadó


Accepted answer seems to be incorrect. Based on what Mozilla says about OO JavaScript, correct way to do it is:

var Person = function(firstName) {
    this.firstName = firstName;
};

function Student(firstName, subject) {
  // Call the parent constructor, making sure (using Function#call)
  // that "this" is set correctly during the call
  Person.call(this, firstName);

  // Initialize our Student-specific properties
  this.subject = subject;
};

// Create a Student.prototype object that inherits from Person.prototype.
// Note: A common error here is to use "new Person()" to create the
// Student.prototype. That's incorrect for several reasons, not least 
// that we don't have anything to give Person for the "firstName" 
// argument. The correct place to call Person is above, where we call 
// it from Student.
Student.prototype = Object.create(Person.prototype); // See note below

// Set the "constructor" property to refer to Student
Student.prototype.constructor = Student;

// Example usage:
var student1 = new Student("Janet", "Applied Physics");

As you can clearly see, Mozilla specifies that it is a common error to use "new Person()" to create the Student.prototype. Hence accepted answer is misleading.

I have actually tested this in my ongoing project and Mozilla's way is correct, while above answer does not work.

like image 28
Siniša Avatar answered Sep 22 '22 05:09

Siniša