Preview ruby-style inheritance using my class library features

Source: Internet
Author: User

Create a new class and use the Dom. Factory method. Obviously, it is a factory method that produces various classes in batch.

 
VaR myfirstclass = Dom. factory ({message: "Hello World", sayhello: function () {alert (this. message) ;}}); var OBJ = new myfirstclass (); obj. sayhello (); // Hello World

Inheritance:

VaR animal = Dom. factory ({init: function (name) {This. name = Name ;}, eat: function () {alert ('yummi') ;}}); var human = Dom. factory ({inherit: Animal, speak: function () {alert (this. name + 'said bla ') ;}}); var Peter = new human ('Peter'); Peter. eat (); // yummie Peter. speak (); // Peter said bla var PAT = Dom. factory ({inherit: human, // note here, use the inherit method init: function (name, age) {This. age = age }}); var PAT = new PAT ('pat ', 18); alert (Pat. name) // Pat alert (Pat. age) // 18 pat. speak (); // Pat said bla

Method chain. That is, the method of the parent class with the same name is called in the current method.

VaR girl = Dom. factory ({sayhello: function () {return "Hello there" ;}}); var fancygirl = Dom. factory ({inherit: Girl, sayhello: function () {return "Well," + this. $ super () + "! ";}}); Var G = new girl; alert (G. sayhello (); var F = new fancygirl; alert (F. sayhello ());

Internal method:

Dom. require ("Lang"); Dom. require ("Oop") var person = Dom. OOP ({init: function (name) {This. name = Name ;}, secret: function () {return 'I sometimes like girly drinks ';}. protect (), // defines it as an internal method. Only describe: function () {return "Hi, I'm # {name} can be called internally }. # {secret }. I kid, I kid. ". instead ({Name: This. name, secret: This. secret ()}) ;}}); var Scott = new person ('situ Zheng mei'); // alert (Scott. secret (); // The method "secret" cannot be called. alert (Scott. describe (); // hi, I'm Scott. I sometimes like girly drinks. I kid, I kid.

Singleton identifies the new production class as a singleton class:

VaR God = Dom. factory ({init: function (name) {This. name = Name; this. alertname = function () {alert (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 alert (God === Lucifer) // alerts true

Alias, alias mechanism:

VaR array2 = Dom. factory ({init: function () {var ARGs = []. slice. call (arguments); this. setarray (ARGs) ;}, setarray: function (ARR) {// converts a common object to an array object of the class, this. length = 0; // you must make it a prototype to use []. push. apply (this, arr); return this;}, tostring: function () {// return a string return []. slice. call (this ). tostring () ;}, valueof: function () {// obtain the array object return []. slice. call (this) ;}, POP: []. pop, push: []. push, shift: []. Shift, unshift: []. unshift, reverse: []. Reverse, indexof: function (El, index) {var n = This. length, I = Index = NULL? 0: index <0? Math. max (0, N + index): Index; For (; I <n; I ++) if (I In this & this [I] = El) return I; Return-1 ;}}); array2.alias ("indexof", "contains") var A = new array2 (1, 2, 3, 4, 5); alert () // test the tostring method alert (. indexof (3) Alert (. contains (3) // test the alias Mechanism

Include, include, similar to Ruby, add prototype members.

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

Extend, extension, similar to Ruby, adding class members.

VaR myclass = Dom. factory ({}); myclass. extend ({Pi: "3.14", alert: function () {alert ("this is a static (class) method")}, alertpi: function () {alert (myclass. pi) ;}}); myclass. alert (); // This is the static (class) method myclass. alertpi (); // 3.14 var M = new myclass M. alert () // error myclass is not defined

Auto scaling and self-Inclusion:

 
VaR module = {selfincluded: function (Klass) {Klass. prototype. boo = 'boo';}, selfextended: function (Klass) {Klass. boo = 'boo' ;}}; var myclass = Dom. factory ({include: module, extend: module}); alert (myclass. prototype. boo); //-> 'boo' alert (myclass. boo); //-> 'boo'
Dom. geometry ={}; var point = Dom. geometry. point = Dom. factory ({init: function (x, y) {This. X = x * 1.0 | 0.0; this. y = y * 1.0 | 0.0;}, tostring: function () {return this. X + ''+ this. Y ;}, clone: function () {return new point (this. x, this. y) ;}, EQ: function (point) {return this. X = point. X & this. y = point. Y ;}, // offset of the moving point to the new position: function (x, y) {If (typeof x === 'object') {Y = x. y; X = x. x;} This. X + = x; this. Y + = y; return this ;}, extend: {fromstring: function (string) {var parts = string. split (/\ s +/); return new point (parts [0], parts [1]) ;}});

Add a prototype member.

 
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 ();
Related Article

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.