JS類の建立と使用

2501 ワード

JS類の建立と使用

//           
document.write("<strong>          </strong><br>"); 
function Person (myName,myAge) { 
//      age,nickName 
this.age = myAge; 
this.nickName = myName; 
//      showInfo 
this.showInfo= function () { 
return(" hi ! my name is "+this.nickName+", I am "+this.age); 
} 
} 

var tom = new Person("Tom",10);//       
var info = tom.showInfo();//    ,       info   
document.write(info+"<br>");//       
document.write(tom.nickName+"<br>");//  tom nickName    
document.write(tom.age+"<br>");//  tom age    

//           
document.write("<strong>          </strong><br>"); 
function Person2 () { 
//       
this.age2; 
this.nickName2; 
//       
this.showInfo2= function () { 
return("   ,      "+this.nickName2+",  "+this.age2+"  。"); 
} 
} 

var tom2 = new Person2();//       
tom2.age2 = 27; 
tom2.nickName2 = "azhou"; 
var info2 = tom2.showInfo2();//    ,          
document.write(info2+"<br>");//       
document.write(tom2.nickName2+"<br>");//           
document.write(tom2.age2+"<br>");//           

//  prototype       1 
document.write("<strong>  prototype       </strong><br>"); 
function Mlist (Myname,Mytime) { 
//  prototype    ,this.myname   Mlist.prototype.myname,          ,           
Mlist.prototype.myname = Myname; 
Mlist.prototype.mytime =Mytime; 
//   .prototype.    =function{}  prototype         , this     
Mlist.prototype.method_name = function() { 
return(Mlist.myname+" "+Mlist.mytime+"      。")	
};	
}; 
Mlist.myname ="azhou"; 
Mlist.mytime =2007; 
document.write("       Mlist.myname   :"+Mlist.myname+"<BR>"); 
document.write("         Mlist.mytime :"+Mlist.mytime+"<BR>"); 
var mymlist_1 =new Mlist("azhou",2007);	
document.write(mymlist_1.method_name()+"<BR>"); 
document.write("        :"+mymlist_1.myname+"<BR>"); 
document.write("        :"+mymlist_1.mytime+"<BR>");