ES5-based JavaScript inheritance

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.