Several ways of writing JS class

Source: Internet
Author: User

We often use the following methods to write a "class" in javascript:

1. Constructors (public properties and methods)
function Person (Iname, iage) {  2: this/  / Public 3: This  //  4: This  . showstudent=function// public 5:  alert (this 6:   7:}


The disadvantage is obvious, the properties and methods of the class are easily modified externally.

The properties and methods above are public. The following example gives the properties and methods of private and public.

2. Constructors (public, private properties and methods)
1:functionPerson (Iname, iage) {2://private Field3:varName =Iname; 4:varAge =iage;5:6://Private Method7:varPrivatefn =function() {8: alert (name); 9:}10:   11:return { 12://Public Field13:name: "Hello" +name,14:age: "Hello" +Age ,15:   16:showstudent:function(){ 17: Privatefn (); 18:alert ( This.  Name); 19:  } 20:  }; 21:}

Called: (New Person ("Xiao", "10")). Showstudent ();

3. Prototyping method (prototype)
function2:c.prototype=3:  name: "Init value A"4:  function  5: This  .name= 6:7: function    8:   This  9:  (new//  output Hello from C, name:init value a 

4. Constructor + Prototype method (prototype)
 1: function   person (iname) { 2: this . Name = Iname;  3:};   4:  5:person.prototype={ 6: GetName: function   () { 7: return  this  .name;  8:}  9:};  10:  One: //  call : var  b = new  person ("Jack") Span style= "color: #000000;" >);  13:alert (B.getname ()); 
most often use the above-mentioned notation. 5. Constructor + Prototype method (prototype)-Save memory notation
1:functionPerson (Iname, iage) {2: This. name=Iname;3: This. age=iage;4:   5://object instances share the same method without causing memory waste6:if(typeofperson._initialized = = "undefined"){ 7:person.prototype.showstudent=function(){ 8:alert ( This. Name); 9:  }; 10:person._initialized=true; 11:  } 12: }  13://called14: (NewPerson ("Jack", "20")). Showstudent ();

The above implementation method, if not _initialized method, can also point to an external function, the same reason.

6. Single-instance (Singleton) mode notation for JavaScript classes
1:varMyNamespace = {}; 2:mynamespace.singleton = (function() { 3:varUniqueinstance;//Private attribute that holds the single instance.4:functionConstructor () {//All of the normal singleton code goes here.5://Private members.6:varPrivateAttribute1 =false; 7:varPrivateAttribute2 = [1, 2, 3]; 8:functionprivateMethod1 () {9://...10:  } 11:functionprivateMethod2 (args) {12://...13:  } 14:return{//Public members .15:publicattribute1:true, 16:publicattribute2:10, 17:PUBLICMETHOD1:function() { 18:// ...19:  }, 20:PUBLICMETHOD2:function(args) {21st:// ...22:  } 23:  } 24:  } 25:return { 26:getinstance:function() { 27:if(!uniqueinstance) {//instantiate only if the instance doesn ' t exist.28:uniqueinstance =Constructor ();29:  } 30:returnuniqueinstance;31:  } 32:  } 33: }) (); 34:   35://Call:36:mynamespace.singleton.getinstance (). PUBLICMETHOD1 ();

Several ways of writing JS class

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.