Prepare for the Salesforce JavaScript Developer Exam. Utilize comprehensive quizzes, flashcards, and multiple choice questions with hints and explanations. Boost your exam readiness!

Practice this question and more.


What is the correct line to initialize a parent constructor in a derived class?

  1. Person.call(this, fName, lName, age);

  2. super(fName, lName, age);

  3. this.constructor(fName, lName, age);

  4. Object.create(Person.prototype);

The correct answer is: Person.call(this, fName, lName, age);

The correct way to initialize a parent constructor in a derived class is by using the method that properly invokes the constructor of the parent class. In the provided choices, the option that uses `Person.call(this, fName, lName, age);` achieves this by explicitly calling the parent constructor function, passing the current context (`this`) as the first argument. This ensures that the properties defined in the parent constructor are set on the instance of the derived class. This approach is commonly used in scenarios involving classic inheritance in JavaScript, particularly prior to the introduction of the `class` syntax. By calling `Person.call(this, ...)`, the properties that `Person` defines for its objects are included in the object created from the derived class. It allows for the correct initialization of inherited properties right when creating an object of the derived class. While calling the parent's constructor with `super(fName, lName, age);` would also be correct in contexts utilizing ES6 class syntax, in classic function-based inheritance, `Person.call(this, ...)` is the appropriate way to initialize the inherited properties. The other listed methods either do not correctly set the derived class instance's context or are not used for initializing the base class constructor directly.