Javascript 静态方法和常规方法

来源:互联网 发布:mac 微软雅黑字体下载 编辑:程序博客网 时间:2024/06/10 02:57

在这里附上静态方法常规方法辨别 

   //创建一个类Employee作为基类

 var Employee = function(name,dept){

       this.name = name ||"none";

       this.dept = dept ||"general";

   }

 Employee.prototype.toString= function(){  //toStringEmployee类的常规方法

       return this.name + "&" + this.dept;

   }

   Employee.show = function(ep){     //showEmployee类的静态方法 

       alert(ep.toString());

   }

   var ep = new Employee("fanrong","技术部");

   Employee.show(ep);         //只能由类调用,不能由实例对象调用.

   //ep.show(ep);           //这样回出错

 

//第二种函数方法

   var Employee =function(name,dept){

       this.name = name ||"none";

       this.dept = dept ||"general";

 showshow =function(){     //这样写showshow函数变成全局函数

         alert("没有var");

     }

  varshowtoo =function(){    //new后,由于作用域因素,showtoo会被释放,

alert("var");           //所以之后使用不了这个方法。

}

this.showThis =function(){   //属于每个实例对象的方法 

 alert(this.name);

}

   }

   Employee.prototype.toString =function(){  //toStringEmployee类的常规方法

       return this.name + "&" + this.dept;

   }

   var ep = new Employee("fanrong","技术部");

 

最后总结,考虑设置什么样的函数,取决你的功能要求,以前5种函数要清楚分清。


原创粉丝点击