In this version, the related APIs are basically stable and will not be changed.
Both the method for calling the parent class and the method for obtaining the parent class are called _ super.
// ================================================ ====// Class factory module //==================================== ============= (function (global, doc) {var dom = global [Doc. URL. replace (/(#. + | \ W)/g, '')]; Dom. define ("class", "Lang", function () {Dom. log ("loaded class module") var P = "prototype", c = "constructor", F = "function", I = "@ init ", S = "_ super", unextend = Dom. oneobject ([S, P, 'extend', 'include ',' _ class']), exclusive = Dom. oneobject ([ S, I, C]), Ron =/on ([A-Z] [A-Za-Z] +)/, classone = Dom. oneobject ('object, array, function'); function expand (Klass, props) {'extend, include '. replace (Dom. rword, function (name) {var modules = props [name]; If (classone [Dom. type (modules)]) {Klass [name]. apply (Klass, []. concat (modules); Delete props [name] ;}}); Return Klass} function setoptions () {var Options = This. options = Dom. object. merge. apply (this. optio NS | {}, arguments), key, match if (typeof this. bind = f) {for (key in options) {If (match = key. match (Ron) {This. BIND (Match [1]. tolowercase (), options [Key]); Delete (options [Key]) ;}}return this;} function _ super () {var caller = arguments. callee. caller; // obtain the current method var name = caller. _ name; // obtain the name of the current method var superclass = caller. _ class [s]; // obtain the parent class of the current instance if (superclass & superclass [p] [name]) {retu Rn superclass [p] [name]. Apply (this, arguments. length? Arguments: caller. Arguments);} else {Throw name + "No super method! "}} Dom [" @ Class "] = {inherit: function (parent, init) {var bridge = function () {} If (typeof parent = f) {for (var I in parent) {// inherits this [I] = parent [I]} bridge [p] = parent [p]; this [p] = new bridge; // inherits the prototype member this [s] = parent; // specify the parent class} This [I] = (this [I] | []). concat (); If (init) {This [I]. push (init);} This. tostring = function () {return (init | Bridge) + "";} var Kp = This [p]; KP. setoptions = Setoptions; Kp [s] = _ super; // bind the method chain return Kp [c] = This;}, include: function () {var target = This [p], bridge ={}; for (VAR I = 0, module; module = arguments [I ++];) {If (Dom. type (module, "object") {Dom. mix (BRIDGE, module);} else if (typeof module = f) {Dom. mix (BRIDGE, new module) ;}} object. keys (BRIDGE ). foreach (function (name) {If (! Exclusive [name]) {var prop = target [name] = bridge [name]; If (typeof prop = f) {prop. _ name = Name; prop. _ class = This ;}}, this); return this ;}, extend: function () {// extension class member var bridge ={} for (VAR I = 0, module; module = arguments [I ++];) {Dom. mix (BRIDGE, module);} For (var key in Bridge) {If (! Unextend [Key]) {This [Key] = bridge [Key]} return this ;}; Dom. factory = function (OBJ) {OBJ = OBJ | |{}; var parent = obj. inherit // parent class var init = obj. init; // constructor Delete obj. inherit; Delete obj. init; var Klass = function () {for (VAR I = 0, Init; init = Klass [I] [I ++];) {init. apply (this, arguments) ;}}; Dom. mix (Klass, Dom ["@ Class"]). inherit (parent, init); // Add more methods return expand (Klass, OBJ ). inclu De (OBJ);} has already been used before (this,this.doc ument ); // 1.7.11 // change Dom ["class"] to Dom ["@ Class"] // 2011.7.25 // The Inheritance chain and method chain are re-implemented. // Call the instance method of the parent class with the same name in the method, from $ super to supermethod, change the prototype property parent of the parent class to superclass // 2011.8.6 // call the instance method of the parent class with the same name in the method, from supermethod to _ super, change the prototype attribute superclass of the parent class to _ super // re-implement the method chain // fix the bug of the sub-class instance that is not the parent class // 2011.8.14 // change the hidden namespace and enhance setoptions
Some tests:
Dom. require ("Ready, Class", function () {var ancestor = Dom. factory ({init: function (name) {This. name = Name ;}, ancestor_prop: "3333333", instance_fn: function () {return "ancestor_instance"}, instance_fn2: function () {return "ancestor_instance2"}, extend: {class_fn: function () {return "ancestor_class" ;}}}); var parent = Dom. factory ({inherit: ancestor, instance_fn: function () {return this. _ super () + "--> parent" ;}, extend: {class_fn: function () {return "parent_class" ;}}}); var son = Dom. factory ({inherit: parent, init: function (name, age) {This. age = age ;}, instance_fn2: function () {return this. _ super () + "--> son" ;}, instance_fn3: function () {return "instance_fn3"}, extend: {class_fn: function () {return "son_class" ;}}}); var P = new parent ("John"); Dom. log (P. instance_fn () Dom. log (parent. class_fn () var S = new son ("nasami", 14); Dom. log (S. instance_fn () Dom. log (S. instance_fn2 () Dom. log (son. class_fn () Dom. log (S. age) Dom. log (S. name) var A = new ancestor ("the Wheel of Time", 30); Dom. log (. age) Dom. log (. name) Dom. log (s instanceof parent )});
Related links:
Dom framework OOP module V5
Dom framework OOP module v4
Dom framework OOP module v3