The simplest object-oriented program
<script type= "Text/javascript" > varnew Object (); = ' 1079161148 '; = ' Mr Lan ' function() { alert (this. Name) } function() { alert (this. QQ) } obj.showname () OBJ.SHOWQQ ()</script>
the method of encapsulation reuse;
<script type= "Text/javascript" >functionCreateperson (name, QQ) {//The constructor constructs an object; varobj =NewObject (); OBJ.QQ=QQ; Obj.name=name Obj.showname=function() {alert (' My name = ' + This. Name)} OBJ.SHOWQQ=function() {alert (' My QQ = ' + This. QQ)} //Factory returnobj}varobj = Createperson (' Mr Lan ', ' 1079161148 '); varObj2 = Createperson (' Mr Li ', ' 1079161168 '); Obj.showname (); OBJ.SHOWQQ ();</script>
//Upgrade-------> constructor call plus new
<script type= "Text/javascript" >functionCreateperson (name, QQ) {//The constructor constructs an object; This. QQ =QQ; This. Name =name This. ShowName =function() {alert (' My name = ' + This. Name)} This. SHOWQQ =function() {alert (' My QQ = ' + This. QQ)} } varobj =NewCreateperson (' Mr Lan ', ' 1079161148 '); varObj2 =NewCreateperson (' Mr Li ', ' 1079161168 '); Obj.showname () obj.showqq ()
Alert (obj.showname () = =Obj2.showname ())//false
</script>
//Advanced Object-oriented programming
<script type= "Text/javascript" >//Mixed constructor Blending (constructor mate prototype) functionCreateperson (name, QQ) {//Constructors Plus Properties This. QQ =QQ; This. Name =name} createPerson.prototype.showName=function(){//prototype Plus methodAlert (' My name = ' + This. Name)} CreatePerson.prototype.showQQ=function() {alert (' My QQ = ' + This. QQ)} varobj =NewCreateperson (' Mr Lan ', ' 1079161148 '); varObj2 =NewCreateperson (' Mr Li ', ' 1079161168 '); //obj.showname ()//can also continue to be used without affecting the use of //obj.showqq ()Alert (obj.showname () = =obj2.showname ())//true</script>
JS Object-oriented constructors