Class factories of various class libraries

Source: Internet
Author: User
Tags mootools
ArticleDirectory
    • Class factory after prototype. js1.6
    • Dojo class factory:
    • Yui class factory
    • Simple JavaScript inheritance
    • Mootools class factory
    • Mass framework class factory

A class factory is a function used to generate classes. We cannot repeat the following for every class we write.Code, Encapsulate it!

 
VaR F = function () {} f. Prototype = superclass. Prototype; subclass. Prototype = new F; subclass. Prototype. constructor = subclass
Class factory after prototype. js1.6
VaR animal = Class. create ({initialize: function (name, sound) {This. name = Name; this. sound = sound;}, speak: function () {alert (this. name + "says:" + this. sound + "! ") ;}}); // Subclassing animalvar snake = Class. create (animal, {initialize: function ($ super, name) {$ super (name, 'hisssssssss') ;}}); var ringneck = new snake ("ringneck "); ringneck. speak (); //-> alerts "ringneck says: hissssssssss! "Var rattlesnake = new snake (" rattler "); rattlesnake. Speak (); //-> alerts" rattler says: hissssssssss! "// Mixing-In enumerablevar animalpen = Class. Create (enumerable, {initialize: function () {var ARGs = $ A (arguments); If (! Args. All (function (ARG) {return Arg instanceof animal}) throw "only animals in here! "This. animals = ARGs;}, // implement _ each to use enumerable Methods _ each: function (iterator) {return this. animals. _ each (iterator) ;}}); var snkepen = new animalpen (ringneck, rattlesnake); snakepen. invoke ('speak'); //-> alerts "ringneck says: hissssssssss! "//-> Alerts" rattler says: hissssssssss!"

Use class. Create to create a class and link a parent class with other materials to form a subclass. To call the parent method with the same name, you need to input a $ super parameter in the parameter of this method.

Dojo class factory:
 
VaR F = function () {} f. Prototype = superclass. Prototype; subclass. Prototype = new F (); subclassprototype. constructor = subclass

Class Definition After prototype. js1.6

 
Dojo. declare ("testclass", null, {ID: "", Info: {name: "", age: ""}, staticvalue: {count: 0}, constructor: function (ID, name, age) {This. id = ID; this.info. name = Name; this.info. age = age this. staticvalue. count ++ ;}});

It has three parameters, class name, parent class, and an object, which contains materials for constructing this class.

Yui class factory
// Http://blog.csdn.net/phphot/article/details/4325823YUI (). use ('oop ', function (y) {var bird = function (name) {This. name = Name;}; bird. prototype. getname = function () {return this. name ;}; var chicken = function (name) {chicken. superclass. constructor. call (this, name) ;}; y. extend (chicken, bird); var chicken = new chicken ('Tom '); Y. log (chicken. getname ());});

Supperclass has two functions: one is to call the method of the parent class, and the other is to call the constructor of the parent class through supperclass. constructor. Two birds in one fell swoop.

However, it is very primitive compared with other class factories and is only responsible for linking child classes and parent classes.

Simple JavaScript inheritance

This is something the author of jquery has done.

// Http://ejohn.org/blog/simple-javascript-inheritance/var person = Class. extend ({init: function (isdancing) {This. dancing = isdancing;}, dance: function () {return this. dancing ;}}); var ninja = person. extend ({init: function () {This. _ super (false) ;}, Dance: function () {// call the inherited version of Dance () return this. _ super () ;}, swingsword: function () {return true ;}}); var P = new person (true); p. dance (); // => truevar n = new Ninja (); N. dance (); // => falsen. swingsword (); // => true // shocould all be truep instanceof person & P instanceof Class & n instanceof ninja & n instanceof person & n instanceof class

Create a parent class by class. Create, and then add an attribute package to create a subclass through the extend method of the parent class.

Mootools class factory
// Http://hmking.blog.51cto.com/3135992/682098 var animal = new class ({initialize: function (AGE) {This. age = age ;}}); var cat = new class ({extends: Animal, initialize: function (name, age) {This. parent (AGE); // callinitalize method of animal class this. name = Name ;}}); var cat = new CAT ('micia ', 20); console. log (cat. name); // 'mica' console. log (cat. age); // 20

It should be the most complex and powerful in all frameworks. The involved APIs include mutator extends implements and extend implement inherited from type, it also uses deep copy to copy the parent class attributes internally!

Extends: implements the parent class, or calls the parent class to initialize this. Parent (). It also overwrites the variables or functions defined by the parent class.

Implements: implements the parent class. Child classes cannot overwrite the methods or variables of the parent class. Even if the subclass defines the same variables or functions as the parent class, it will be replaced by the parent class.

Implement: it is a prototype member used to adjust the created class.

Extend: Call the extend method of the subclass (not its instance) to create a new subclass.

Mass framework class factory
// Http://rubylouvre.github.com/doc/index.html#.require ("class, spec", function () {var shape = $. factory ({init: function (LEN) {$. log (LEN) This. length = Len | 0;}, getlength: function () {return this. length;}, setlength: function (LEN) {This. length = Len;}, getarea: function () {Throw "subclasses must implement this method"}) var triangle = $. factory ({inherit: shape, init: function (Len, hei) {// The LEN attribute is defined in the parent class. This can be omitted here. height = Hei | 0;}, getarea: function () {return this. length * This. height/2}) var square = $. factory ({inherit: shape, getarea: function () {return this. length * This. length ;}}); var T = New Triangle (3, 4) $. log (T. getarea (), true) var S = New Square (4) $. log (S. getarea (), true )});

$. Factory is a class factory and the parameter is a common object. This object has the following optional attributes:

    • Init is the new class constructor. No empty function is passed in by default.
    • Inherit is the parent class of the new class.
    • Extend parameters are an object or an object array. In any case, the attributes of these objects only add static members to the new class, or they are added to the class.
    • The Implement parameter is an object or an object array or class (class is a function). The attributes of these objects only add instance members to the new class, or they are added to the class prototype.

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.