There are many methods for JavaScript inheritance, including compile-to-javascript TypeScript, CoffeeScript, and various polyfill implementations provided by the website MDN, GitHub, and Modernizr.
From the perspective of ES5, some of these solutions are functional, but the semantics is not satisfactory.
I have taken some trendy ideas from these schemes and compiled a scheme to achieve the same style as that inherited by the native DOM class and achieve both functional and semantic effects (of course, don't always think about ES3 after 99 ).
If your WebApp is based on the ES5 runtime environment, you can evaluate or fork this solution.
// Exports Function. prototype. extends // exports global. getPrototypeNames // implement the Function based on ES5 inheritance. prototype. extends = (function () {function getOwnPropertyDescriptors (object) {return Object. getOwnPropertyNames (object ). reduce (function (PPS, pn) {return PPS [pn] = Object. getOwnPropertyDescriptor (object, pn), pd;},{}) ;}/ *** inherits a class. * If there are subsequent classes, the subsequent attributes of the subsequent classes will be shared (except for constructor). * getters in these attributes, setters and methods must take into account the common (such as those of the Array methods) **/function inherits (s) {var c, p; c = this; if (typeof s = "function") {p = Object. create (s. prototype, getOwnPropertyDescriptors (c. prototype);} else {p = c. prototype;} if (arguments. length> 1) {Array. prototype. slice. call (arguments, 1 ). forEach (function (s) {var PPS = getOwnPropertyDescriptors (s. prototype); delete. const Ructor; Object. defineProperties (p, pps) ;});} c. prototype = p;} return inherits ;}()); // test preparation //~~~~~~~~~~~~~~~~~~~~~~~~ // BaseList extends Array //~~~~~~~~~~~~~~~~~~~~~~~~ Function BaseList () {this. length = this. length;} BaseList. prototype. add = function (e) {return this. push (e) ;}; BaseList. extends (Array );//~~~~~~~~~~~~~~~~~~~~~~~~ // ItemList extends BaseList //~~~~~~~~~~~~~~~~~~~~~~~~ Function ItemList () {BaseList. call (this);} ItemList. extends (BaseList, EventTarget); ItemList. prototype. item = function item (index) {index >>> = 0; return index
"Required parameter list.push(document.doc umentElement); list. push (document. head); console. assert (list. item (1) = document. head, "The second item of list is document. head ");