在这里附上静态方法和常规方法辨别
//创建一个类Employee作为基类
var Employee = function(name,dept){
this.name = name ||
"none";
this.dept = dept ||
"general";
}
Employee.prototype.toString
= function(){ //toString是Employee类的常规方法①
return this.name + "&" + this.dept;
}
Employee.show = function
(ep){
//show是Employee类的静态方法
②
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");
}
var
showtoo =
function(){
//在new后,由于作用域因素,showtoo会被释放,
alert("有var");
//所以之后使用不了这个方法。
④
}
this.showThis =
function(){
//属于每个实例对象的方法
⑤
alert(this.name);
}
}
Employee.prototype.toString =
function(){ //toString是Employee类的常规方法
return this.name + "&" + this.dept;
}
var ep = new Employee("fanrong","技术部");
最后总结,考虑设置什么样的函数,取决你的功能要求,以前5种函数要清楚分清。