The realization of the decorative model of mannose model

Source: Internet
Author: User
Tags class definition constructor wrapper

This is the second time I've made a project on the Nectar model. This time it is based on the latest version of the mannose model to do design patterns to explore the implementation.

Before you begin, say a few words:

JavaScript is running on top of the browser, and design patterns are commonly used in large projects, which combine to produce a center, two basic points.

The so-called One center: stable operation, performance first.

Browser to run the program, want to bigger projects, the first is to pass the performance of this. The Mannose model realizes the perfect memory resource saving and performance optimization, which is the core of the architecture.

The so-called two basic points: 1, clear structure, 2, easy to expand;

Based on the Nectar model itself, object-oriented thinking, can be used to achieve GOF23, so, in the framework and expansion since the design pattern to pave the way, there is no need to worry about a big problem.

The code example describes the decorative mode C # version from Terrylee, http://terrylee.cnblogs.com/archive/2006/03/01/340592.html

For a detailed explanation of the decoration mode, please see Terrylee's decorative Mode C # version, here I do not more detailed.

Nonsense finished, directly on the code:

============ mannose model begins =================================

    Define the syntax nectar of a class: Class ()//The last parameter is the definition of the classes represented by JSON//if the number of arguments is greater than 1, then the first parameter is the base class//First and last parameter, and in the future, the interface//return value of the class implementation can be expressed as a class, which is a
      constructor function Class () {var adefine = arguments[arguments.length-1];//The last parameter is class definition if (!adefine) return; var abase = arguments.length > 1? Arguments[0]: object; Resolves base class function Prototype_ () {}; Construct the temporary function of prototype to hook up the prototype chain prototype_.prototype = Abase.prototype;  Ready to pass prototype var aprototype = new Prototype_ ();
        Create a class to use the prototype for (Var adefine)//copy class defined to the current class prototype if (member!= "Create")//constructors do not copy
      Aprototype[member] = Adefine[member]; Depending on whether special properties and performance are inherited, the following statements can be commented out separately if (adefine.tostring!= Object.prototype.toString) aprototype.tostring = Adefi
      ne.tostring; 
      if (adefine.tolocalestring!= Object.prototype.toLocaleString) aprototype.tolocalestring = adefine.tolocalestring; if (adefine.valueof!= Object.prototype.valueOf) aprototype.valueof =adefine.valueof; if (adefine.create)//If there is a constructor var atype = adefine.create//type is that constructor else//Otherwise the default constructor atype = Functi  On () {this.base.apply (this, arguments);
      Call base class constructor};  Atype.prototype = Aprototype;       Sets the prototype Atype.base = Abase of the class (constructor);  Set type relationship, easy to trace the inheritance relation aType.prototype.Type = Atype;  Extend a Type property for this class object return atype;
    Returns the constructor as a class}; Root class object definition: Function object () {}//define lowercase object root class, used to implement the most basic method, etc. object.prototype.isA = function (atype)//Judge whether the object belongs to to a type {var self = this.
      Type;
        while (self) {if (self = = Atype) return true; Self = self.
      Base;
      };
    return false;
    }; Object.prototype.base = function ()//Call base class Constructor {var base = this. Type.base; Gets the base class of the current object if (! base.base)//Jocky class no longer has a base class base.apply (this, arguments)//directly calls the base class constructor else//Jocky class and base class {this.b   ASE = Makebase (Base); First Overwrite this.base Base.apPly (this, arguments);        Then call the base class constructor delete This.base;
      Deletes the overridden base property};
        function Makebase (type)//wrapper base class Constructor {var base = Type.base; if (! Base.base) return Base;   Base class no longer has a base class, there is no need to wrap the return function ()//wrapper as a closed package that refers to the temporary variable base {this.base = Makebase (base);  First overwrite this.base base.apply (this, arguments);
      Then call the base class constructor};
    }; };

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.