以前看到的一个object的写法,当时有全部JS都用这样的类的方式去写的冲动
1 <script type="text/javascript"> 2 function Student(){ 3 this.id=0; 4 this.name=""; 5 this.age=0; 6 } 7 8 Student.prototype.GetName=function(){ 9 return this.name; 10 } 11 12 Student.prototype.SetName=function(value){ 13 this.name=value; 14 } 15 16 Student.prototype.ToString=function(){ 17 return "编号:"+this.id+"姓名"+this.name+"年龄"+this.age 18 } 19 </script>
在页面上调用
1 <script type="text/javascript"> 2 var stud=new Student(); 3 stud.SetName("Jack"); 4 alert(stud.ToString()); 5 </script>