Search code examples
javascriptclasscallsuper

What is the proper use of super.call(this)


I have a class function (not originally developed by me)...

function Project()
{
  Project.super.call(this);
  // this._some_var = null;
  /* other initial vars */
  ...
  return DefensiveObject.create(this); // see comment below
}

function initialize()
  {
     //*******Initialize Project************
  }
  ...

return Project;

This function is part of a module called "Project.js" included by running node main.js.

  return DefensiveObject.create(this); // not return Object.create(this)

DefensiveObject is a class to prevent objects from getting or setting properties that are not explicitly setup in the class.

The main.js calls Project.initialize() which resides within my Project class.

My question is why would there be a need to call "Project.super.call(this);"?


Solution

  • in Javascript the reserved word super is used on ES6 classes for referencing the parent of a child class, it doesn't makes sense to use it referencing a function.

    please read this article where the usage of super is explained

    https://jordankasper.com/understanding-super-in-javascript/

    also this medium article can help you https://medium.com/beginners-guide-to-mobile-web-development/super-and-extends-in-javascript-es6-understanding-the-tough-parts-6120372d3420