staticMethod
by xiaoji121
JavaScript
// 构造函数
var Gadget = function(price){
this.price = price;
};
// 静态方法
Gadget.isShiny = function(){
var msg = "you bet";
console.log(this);
if(this instanceof Gadget){
// this only works if called non-statically
msg += ", it costs $" + this.price + "!";
}
return msg;
};
// 向该原型中添加一个普通的方法
Gadget.prototype.isShiny = function(){
return Gadget.isShiny.call(this);
}
alert(Gadget.isShiny());// you bet
var a = new Gadget('499.99');
alert(a.isShiny());// you bet, it costs $499.99!