Common tool functions for JS combination inheritance

Source: Internet
Author: User

This tool function does not make sense, just because the extend method of ext is not well understood, wrote a simplified extend method to help understand.

/** *  */e = {}; E.extend = function (Sub, SUP) {//Borrow constructor Sub.prototype = sup;//Preserve the constructor of the parent class in order to use the call in the subclass constructor, Bind the parent class variable under this sub.prototype.superclass = sup.constructor;//because the constructor is overridden so that constructor is re-specified, So that instanceof behaves normally sub.prototype.constructor = sub;//because the variable is bound to a subclass, delete the overridden variable for (var i-in sup) {if (typeof Sup[i]!== ' F on the prototype) Unction ') {delete sup[i];}} return sub;}; E.interfacefactory = {}; E.interfacefactory.createinterface = function (methods) {var Interface = function () {};var F = typeof arguments[0] = = = ' str ing '; var p = f? Arguments:arguments[0];var len = P.length;var _proto = Interface.prototype = {};for (var i = 0; i < len; i++) {_proto . p[i] = function () {throw new Error (' no implements function ');};} return new Interface ();};


Test

var person = function (name) {this.name = name;} Person.prototype.say = function () {alert (' I am ' + this.name);} var SS = function (name, age) {This.superclass.call (this,name); this.age = age;} SS = E.extend (ss, New Person (' sz ')), var s = new SS (' xx ', one-to-one); S.say ();



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.