recently, I upgraded the class factory and added many time-consuming functions for it, such as the method chain and extend subclass producer.
// ================================================ ====// Class factory module V12 by situ zhengmei // ========================== ===================== define ("class ", ["Lang"], function ($) {function bridge () {} var fntest =/mass /. test (function () {mass ;})? /\ B _super | _ superapply \ B /:/. */; var hash = {inherit: function (parent, init) {// inherit a parent class and put it into the _ init list, and add the setoptions prototype method if (typeof parent = "function") {for (var I in parent) {// inherit class member this [I] = parent [I];} bridge. prototype = parent. prototype; this. prototype = new bridge; // inherits the prototype member this. _ super = parent; // specify the parent class if (! This. _ init _) {This. _ init _ = [Parent]} This. _ init _ = (this. _ init _ | []). concat (); If (init) {This. _ init __. push (init);} This. tostring = function () {return (init | Bridge) + "";} var proto = This. fn = This. prototype; Proto. extend = hash. extend; Proto. setoptions = function () {var first = arguments [0]; If (typeof first = "string ") {First = This [first] | (this [first] ={}); []. splice. call (arguments, 0, 1, first);} else {[]. unshift. call (arguments, this);} $. object. merge. apply (null, arguments); return this;} return Proto. constructor = This;}, extend: function (module) {// Add a set of prototype Methods var target = This; object. keys (module ). foreach (function (name) {var fn = target [name], FN2 = module [name] If (typeof fn = "funciton" & typeof FN2 = "function" & fntest. test (FN2) {VaR _ super = function () {// create a method chain return fn. apply (this, arguments) ;}; VaR _ superapply = function (ARGs) {return fn. apply (this, argS) ;}; target [name] = function () {var T1 = This. _ super; var t2 = This. _ superapply; this. _ super = _ super; this. _ superapply = _ superapply; var ret = fn2.apply (this, arguments); this. _ super = T1; this. _ superapply = t2; return ret ;};} else {target [name] = FN2 ;}}); return this ;}}; function getsubclass (OBJ) {return $. factory (this, OBJ);} $. factory = function (parent, OBJ) {If (arguments. length = 1) {OBJ = parent; parent = NULL;} var statics = obj. statics; // static member extension package var init = obj. init; // constructor Delete obj. init; Delete obj. statics; var Klass = function () {for (VAR I = 0, Init; init = Klass. _ init _ [I ++];) {init. apply (this, arguments) ;}}; hash. inherit. call (Klass, parent, init); // inherits the parent class prototype members and class members var fn = Klass. FN; VaR _ init _ = Klass. _ init __; $. mix (Klass, statics); // Add a class member Klass. prototype = Klass. fn = FN; Klass. _ init _ = _ init __; Klass. FN. extend (OBJ); Klass. mix = $. mix; Klass. extend = getsubclass; return Klass;}; $. mix ($. factory, hash); return $ });
example:
require ("class", function ($) {var animal = $. factory ({init: function (name) {This. name = Name}, getname: function () {return this. name ;}, setname: function (name) {This. name = Name ;}}); var A = new animal ("Zzz"); var dog =$. factory (animal, {init: function (name, age) {This. age = age ;}, getage: function () {return this. age ;}, setage: function (AGE) {This. age = age ;}}); var dog = new dog ("dog", 222); console. log (DOG) ;})