JavaScript class definition Summary

Source: Internet
Author: User

A variety of JavaScript definition classes can be found online:

1.ECMAScript defining a Class or object

Three ways to define classes (class) in 2.Javascript

I want to sort out a "concise" class definition template that implements the modularity of the code.

First, the name space

// Root Namespace var Root; if (! Root) root={}; .. // Level Two namespaces if (! Root.secondns) root.secondns={};

Such.

Ii. definition of Class

Implement code modularity by defining classes <script>

        //#region class: MyClass1 (Support folding: details click)        varmyclass1=function () {//Public Property             This. publicprop1=1; //Private Property             This. PRV = {};  This. PRV.PROP1 =2; //method, static property initialization            if(typeofmyclass1._initialized = ="undefined") {                //Method of publicmyclass1.prototype.publicop1=function () { This. PRV.PROP1 + =5;  This. PUBLICPROP1 + =6; Console.log ("privateprop1="+ This. prv.prop1+", publicprop1="+ This. PublicProp1); }                //Method of PrivateMYCLASS1.PROTOTYPE.PRV = {}; MYCLASS1.PROTOTYPE.PRV.OP1=function () {}//static properties (accessed through the class name)myclass1.staticprop1="STATICPROP1"; Myclass1._initialized=true; }        }        //#endregion class: MyClass1//Test        varx =NewMyClass1 (); X.PUBLICOP1 ();        vary =NewMyClass1 (); Y.PUBLICOP1 ();

        Console.log ("myclass1.staticprop1=" + myclass1.staticprop1);        myclass1.staticprop1= "NewValue"
Console.log ("myclass1.staticprop1=" +</script>

Iii. inheritance

Do not consider

Iv. Hierarchy of modules

Through the combination of namespace and class, the program module is implemented hierarchically.

// Root Namespace root={root attribute 1: attribute value, Root method 1:function () {...}, class 1:function () {Class 1 definition), Class 2:new function () {Class 2 definition),/ / single / / Two-level namespace secondns:{...};

JavaScript class definition Summary

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.