I've been having issues with the structure of my vanilla js code, with regards to local/global variables/methods, inheritance, etc.
my search for a good example of code found this script:markerwithlabel.js
im curious about this bit:
/**
* @param {Function} childCtor Child class.
* @param {Function} parentCtor Parent class.
* @private
*/
function inherits(childCtor, parentCtor) {
/* @constructor */
function tempCtor() {}
tempCtor.prototype = parentCtor.prototype;
childCtor.superClass_ = parentCtor.prototype;
childCtor.prototype = new tempCtor();
/* @override */
childCtor.prototype.constructor = childCtor;
}
How can one use this code in personal projects? what does it do exactly for you.
apparently this snippet is widely used: https://github.com/search?p=5&q=%22function+inherits%28childCtor%2C+parentCtor%29+%7B%22&type=Code
This is the prototypal way of performing inheritance in javascript. This function
inherits(childCtor, parentCtor)
simply passes properties and features fromparentCtor
tochildCtor
. It's an object oriented principle of sharing features between related objects. This code does exactly whatextends
in es2015 does in objects. Please refer to my comments below, I tried to be open and not more technical.I just wanted to make it quick, You can read further more on this topic here