the new version is restructured on a large scale, and the inheritance chain and method chain are re-implemented. In the method, call the instance method with the same name as the parent class, change from $ super to supermethod, and change the prototype attribute parent of the parent class to superclass.
// "Use strict"; (function (global, DOC) {var dom = global [Doc. URL. split ("#") [0]; Dom. define ("class", "Lang", function () {// ============================================== ====/// the first type of factory in the core module /// ================================ ===================== var P = "prototype ", C = "constructor", F = "function", I = "@ init", S = "superclass", unextend = Dom. oneobject ([P, S, 'extend', 'include ', 'tostring', "supermethod"]), exclusive = Dom. Oneobject ([C, I]), empty = function () {}, 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} Dom ["@ Class"] = {inherit: function (parent, init) {If (typeof parent = f) {for (VA R I in parent) {// inherit class methods and attributes this [I] = parent [I]} Dom. mix (this [p], parent [p]); // inherit the prototype method and attribute this [s] = parent;} // Add obj. extend, obj. include objects or classes in this. tostring = Dom. K (init | empty) + ""); this [p]. supermethod = function () {var callee = arguments. callee; var caller = callee. caller; If (caller & caller. _ method) {return caller. _ method. apply (this, arguments) }}var Kp = This [p]; Kp [I] = (Kp [I] | []). Slice (); If (typeof init = f) {Kp [I]. push (init);} KP. setoptions = function () {This. options = Dom. object2.merge. apply (this. options | {}, arguments); return this;} return Kp [c] = This;}, extend: function () {// extension class member var middleware ={} for (VAR I = 0, module; module = arguments [I ++];) {Dom. mix (middleware, module);} object. keys (middleware ). foreach (function (name) {If (! Unextend [name]) {This [name] = middleware [name] ;}}, this); return this ;}, include: function () {// extended prototype member var son = This [p], parent = (this [s] | function) [p], middleware = {}, _ method, method; for (VAR I = 0, module; module = arguments [I ++];) {If (Dom. type (module, "object") {Dom. mix (middleware, module);} else if (typeof module = f) {Dom. mix (middleware, new module) ;}} object. keys (middleware ). foreac H (function (name) {If (! Exclusive [name]) {_ method = parent [name]; method = middleware [name]; son [name] = method; if (typeof Method = F & typeof _ Method = f) {son [name]. _ method = _ method ;}}); 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 = This [I] [I + +];) {Init. apply (this, arguments) ;}}; Dom. mix (Klass, Dom ["@ Class"]). inherit (parent, init); // Add more methods return expand (Klass, OBJ ). include (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 with the same name as the parent class in the method, change from $ super to supermethod, and change the prototype attribute parent of the parent class to superclass.
some tests:
Dom. require ("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. supermethod () + "--> 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. supermethod () + "--> 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", 44); Dom. log (S. instance_fn (); Dom. log (S. age); Dom. log (S. name); Dom. log (S. instance_fn2 (); Dom. log (son. class_fn ());});
Related links:
Dom framework OOP module v4
Dom framework OOP module v3