Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

javascript - 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);"?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...