gleams 发表于 2013-1-22 22:36:10

javascript类的创建的多种方式

基本方法很简单,但在javascript中创建类有多种方式。
看例子代码,主要是类方法的写法不一样。
1. function people(name){
          this.name=name;
          this.printInfo=function(){document.write(this.name);};
  }
  
2. 
   var people=function(name){
   this.name=name;
   this.printInfo=function(){document.write(this.name)}
  }
 
3.
  var people=function(name){
   this.name=name;
   this.printInfo=printInfo;
  }
  
  function printInfo(){document.write(this.name)}
 
4.  var people=function(name){
   this.name=name;
  }
  
  people.prototype.printInfo=function(){document.write(this.name)}
 
5.  var people=function(name){
   this.name=name;
  }
  
  people.prototype={
   printInfo:function(){document.write(this.name)}
  }
 
 
页: [1]
查看完整版本: javascript类的创建的多种方式