Mass framework class module V7

Source: Internet
Author: User

The class factory module of Dom framework makes it easy for people to design robust classes and a clear hierarchy inheritance system, which plays a significant role in building the UI library.

The improvements of this version abandon the ruby-style include API and change its name to implement, approaching javaer. At the same time, a bug in the implement method is fixed, allowing users to rewrite the tostring and valueof two special prototype methods.

// ================================================ ====// 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", I = "@ init", S = "_ super", unextend = Dom. oneobject ([S, P, 'extend', 'enablement', '_ class']), exclusive = new Regexp ([s, I, C]. join (" | "), Ron =/on ([A-Z] [A-Za-Z] +)/, classone = Dom. oneobject ('object, array, function'); function expand (Klass, props) {'extend, implement '. 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. option S | {}, arguments), key, match if (typeof this. bind = "function") {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]) {Return 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 =" function ") {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. se Toptions = setoptions; Kp [s] = _ super; // bind the method chain return Kp [c] = This;}, implement: function () {var target = This [p], bridge ={}, Reg = exclusive; For (VAR I = 0, module; module = arguments [I ++];) {If (Dom. type (module, "object") {Dom. mix (BRIDGE, module);} else if (typeof module = "function") {Dom. mix (BRIDGE, new module) ;}} object. keys (BRIDGE ). foreach (function (name) {If (! Reg. test (name) {var prop = target [name] = bridge [name]; If (typeof prop = "function") {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 ). imple Ment (OBJ);} specify multiple parameter values (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, enhance setoptions // 2011.10.7 include and rename it implement to fix the implement bug (the tostring valueof method can be rewritten)

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 v6
Dom framework OOP module V5
Dom framework OOP module v4
Dom framework OOP module v3

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.