Dom framework OOP module v2

Source: Internet
Author: User

The entire framework is being restructured so that the namespace object Dom is also out of the same inheritance system, just like the type object of mootools1.3.

The objective of V2 is roughly as follows:

    1. Reduces modularity and only retains ecma262v5 and the most useful extensions. The Lang module will be integrated with the core module.
    2. The module is a pure object, removing the protect method that is of little use, and implementing extend and include can call the parent Method
    3. Remove the function of intelligently calling the parent constructor In the constructor, and then call the parent method as this. _ super ()
    4. Added the parameter that can be instantiated without using the new keyword (must be configured in the configuration object)
// By situ zhengmei http://www.cnblogs.com/rubylouvre/ 2010.14var proto = "prototype", ctor = "constructor", a_proto = array [proto], a_slice = a_proto.slice, to_s = object [proto]. tostring, is = function (OBJ, type) {return (type = "object" & OBJ = Object (OBJ )) | (type = "array" & array. isarray & array. isarray (OBJ) | // ECMA-5 15.4.3.2 (type = "null" & OBJ = NULL) | (type = "undefined" & ob J = void 0) | OBJ & to_s.call (OBJ ). slice (8,-1) ==== type;} function extend (target, source) {for (VAR name in source) if (source. hasownproperty (name )&&! Target [name]) {target [name] = source [name];} return target;} // object extension // fix ie .. in bugvar _ dontenum = ['propertyisenumerable', 'isprototypeof ', 'hasownproperties', 'tolocalstring', 'tostring', 'value', 'constructor']; for (var I in {tostring: 1}) _ dontenum = false; // The second parameter only supports objects in the browser. extend (object, {create: function (PROTO, props) {// ecma262v5 15.2.3.5 var ctor = function (PS) {If (PS & object. defineproperties) object. defineproperties (this, PS) ;}; ctor [proto] = proto; return New ctor (props) ;}, keys: function (OBJ) {// ecma262v5 15.2.3.14 var result = [], dontenum = _ dontenum, length = dontenum. length; For (var key in OBJ) if (obj. hasownproperty (key) {result. push (key)} If (dontenum) {While (length) {key = dontenum [-- length]; If (obj. hasownproperty (key) {result. P Ush (key) ;}}return result ;}}); // function iterator (vars, body, RET) used to create the javascript1.6 Array) {return eval ('[function (FN, scope) {' + 'For (VAR' + vars + 'I = 0, L = This. length; I <L; I ++) {'+ body. replace ('_', 'fn. call (scope, this [I], I, this) ') +'} '+ RET +'}] ') [0];}; // copy the comments to the extend (array [proto], {// locate the class to return the first index of the specified item. Indexof: function (El, index) {var n = This. length, I = ~~ Index; if (I <0) I + = N; For (; I <n; I ++) if (this [I] = El) return I; return-1 ;}, // The positioning class returns the last index of the specified item. Lastindexof: function (El, index) {var n = This. length, I = Index = NULL? N-1: Index; if (I <0) I = math. max (0, N + I); For (; I> = 0; I --) if (this [I] = El) return I; Return-1 ;}, // The iteration class runs a function on each item in the array. If all results return the true value, this method also returns the true value. Foreach: iterator ('', '_',''), // The iteration class runs a function on each item in the array, and returns the item that returns the true value of the function as an array. Filter: iterator ('R = [], j = 0, ', 'If (_) R [J ++] = This [I]', 'Return R '), // The iteration class runs a function on each item in the array and returns all results as an array. Map: iterator ('R = [], ', 'r [I] = _', 'Return R'), // The iteration class runs a function on each item in the array, if any result returns true, the true value is returned. Some: iterator ('', 'If (_) return true', 'Return false'), // The iteration class runs a function on each item in the array, if all results return the true value, this method also returns the true value. Every: iterator ('', 'If (! _) Return false', 'Return true'), // The normalization class javascript1.8 runs a function on each entry of the array and the result of the previous call to collect the final result. Reduce: function (FN, lastresult, scope) {If (this. Length = 0) return lastresult; var I = lastresult! = Undefined? 0: 1; var result = lastresult! = Undefined? Lastresult: This [0]; for (VAR n = This. length; I <n; I ++) Result = fn. call (scope, result, this [I], I, this); return result ;}, // The same as cript1.8 for the native class, but it is executed from the right to the left. Reduceright: function (FN, lastresult, scope) {var array = This. concat (). reverse (); Return array. reduce (FN, lastresult, scope) ;}}); // corrected the problem of unshift not returning array length in ie67 // http://www.cnblogs.com/rubylouvre/archive/2010/01/14/1647751.htmlif ([]. unshift (1 )! = 1) {a_proto.unshift = function () {var ARGs = [0, 0]; for (VAR I = 0, n = arguments. length; I <n; I ++) {ARGs [args. length] = arguments [I]} a_proto.splice.apply (this, argS); return this. length; // return the length of the new array} // string extension var metaobject = {'\ B': '\ B', '\ t ': '\ t',' \ N': '\ n',' \ F': '\ F',' \ R': '\ R ', '"': '\"', '\': '\'}, rquote =/[\ x00-\ x1f \]/g; extend (string [proto], {// javascript1.5 Firefox has implemented quote: Function () {var STR = This. Replace (rquote, function (CHR) {var meta = metaobject [CHR]; return meta? Meta: '\ U' + ('000000' + Chr. charcodeat (0 ). tostring (16 )). slice (-4);}); Return '"' + STR + '"';}, // ecma262v5 15.5.4.20 // http://www.cnblogs.com/rubylouvre/archive/2009/09/18/1568794.html trim: function () {var STR = This. replace (/^ (\ s | \ u00a0) +/, ''), Ws =/\ s/, I = Str. length; while (WS. test (Str. charat (-- I); Return Str. slice (0, I + 1) ;}}); // math extension // http://www.cnblogs.com/rubylouvre/archive/2010/10 /09/1846941 .html var native_random = math. random; math. random = function (Min, Max, exact) {If (arguments. length = 0) {return native_random ();} else if (arguments. length = 1) {max = min; min = 0;} var range = min + (native_random () * (max-min )); return exact = void (0 )? Math. round (range): range. tofixed (exact);} extend (function [proto], {// ecma262v5 15.3.4.5 BIND: function (scope) {If (arguments. length <2 & scope = void 0) return this; var fn = This, argv = arguments; return function () {var ARGs = [], I; for (I = 1; I <argv. length; I ++) args. push (argv [I]); for (I = 0; I <arguments. length; I ++) args. push (arguments [I]); Return fn. apply (scope, argS) ;}}}); Function _ Numarr (s) {// auxiliary function var r = [], K =-1, I = 0, J, A = S. split (""), Z =. length; For (; I <z; ++ I) {for (j = 0; j <z; ++ J) {R [++ K] = A [I] + A [J] ;}} return r ;}var numarr = _ numarr ("0123456789"); function toisostring () {varMS = This. getutcmilliseconds (), pad0 = (MS <10 )? "00": (MS <100 )? "0": ""; return this. getutcfullyear () + '-' + numarr [this. getutcmonth () + 1] + '-' + numarr [this. getutcdate ()] + 'T' + numarr [this. getutchours ()] + ':' + numarr [this. getutcminutes ()] + ':' + numarr [this. getutcseconds ()] + '. '+ pad0 + this. getutcmilliseconds () + 'Z';} extend (date [proto], {// ecma262v515.9.5.43 toisostring: toisostring, // ecma262v5 15.9.5.44 tojson: toisostring}); Extend (date, date, {// ecma262v5 15.9.4.4 now: function () {return new date (). valueof ();}});

Brand new class factory.

VaR oneobject = function (array, Val) {var result = {}, value = Val! = Void 0? VAL: 1; for (VAR I = 0, n = array. length; I <textarea style="width:100%" onkeyup="  this.rows=(function(t){    for (var i=0,h=1;i<t.length;i++) if (t.charAt(i) == '\n') h++;    return h;  })(this.value);">// --------------------- This is an example ----- var myfirstclass = OOP ({message: "Hello World", sayhello: function () {P (this. message) ;}}); var OBJ = new myfirstclass (); obj. sayhello (); // Hello World //-----------------------------------</textarea>  Run Clear 

Extension class members and calling the superclass in class methods:

 
VaR mymath = OOP ({}); mymath. extend ({Pi: 3.14, getpi: function () {return this. pi ;}}); var sonmath = OOP ({inherit: mymath}); sonmath. extend ({getpi: function () {return this. _ super () + 0.0015926 ;}}); P (sonmath. getpi ());

Extended prototype members. Attributes of extend and include can be single objects or object arrays.

 
VaR movable = {run: function () {P ("can run")}, Fly: function () {P ("can fly")} var recognition = {watch: function () {P ("")}, smell: function () {P ("")} var robot = OOP ({init: function (name, type) {This. name = Name; this. type = Name ;}, include: [movable, recognition]}); var Chi = new robot ("", "chobits"); P (chi. name); chi. watch (); chi. fly ();

Configure the singleton class

VaR God = OOP ({init: function (name) {This. name = Name; this. alertname = function () {P (this. name) }}, singleton: True // note here, use the singleton attribute}); var God = new god (""); God. alertname (); // alerts var Lucifer = new god ("Satan"); Lucifer. alertname (); // alerts P (God = Lucifer) // alerts true

Configuration can be instantiated without the new keyword.

 
VaR dom2 = OOP ({init: function (selector) {This. selector = selector return this;}, nonew: True, getselectors: function () {return (this. selector | ""). split (/\ s +/)}); P (dom2 ('. AAA. bbb. CCC '). getselectors ())

Inheritance Example 1

VaR animal = OOP ({init: function (name) {This. name = Name ;}, getfood: function () {return "various foods"}, extend: {getclassname: function () {return "animal ";}}}); vaR human = OOP ({inherit: Animal, extend: {getclassname: function () {return this. _ super () + "--> Persons" ;}}}); var me = new human ("John"); P (human. getclassname (); P (Me. getfood (); var man = OOP ({inherit: human, init: function (name, sex) {This. _ super (); this. sex = sex;}, getfood: function () {return this. _ super () + ", especially meat" ;}}); var genghis_khan = new man ("Genghis Khan", "male"); P (genghis_khan.sex ); P (genghis_khan.name); P (genghis_khan.getfood ());

Inheritance Example 2

VaR polygon = OOP ({init: function (sides) {This. sides = sides}, getarea: function () {return 0 // This is only an abstract class and cannot be used for specific calculations }}); P ("================== triangle ================== ") vaR triangle = OOP ({inherit: polygon, init: function (base, height) {This. _ super (3); this. base = base; this. height = height;}, getarea: function () {return 0.5 * This. base * This. height ;}}); var T = New Triangle (); P (t. sides); P (t. getarea (); P ("================= rectangle ===================== ") vaR rectangle = OOP ({inherit: polygon, init: function (length, width) {This. _ super (4); this. length = length; this. width = width ;}, getarea: function () {return this. length * This. width ;}}); var r = new rectangle (7,6); P (R. sides); P (R. getarea (rectangle); P (r instanceof polygon) P ("================== square ================== ") vaR square = OOP ({inherit: rectangle, init: function (side) {This. _ super (side, side) ;}}) var S = New Square (6); P (S. sides); P (S. getarea () P (S instanceof polygon) P (S instanceof square)

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.