我的困惑從這個代碼片斷的最後一行莖:理解「靜態」的方法
function Animal(name) {
Animal.count = Animal.count+1||1;// static variables, use function name "Animal"
this.name = name; //instance variable, using "this"
}
Animal.showCount = function() {//static method
alert(Animal.count)
}
Animal.prototype.showName=function(){//instance method
alert(this.name);
}
var mouse = new Animal("Mickey");
var elephant = new Animal("Haddoop");
Animal.showCount(); // static method, count=2
mouse.showName();//instance method, alert "Mickey"
mouse.showCount();//Error!! mouse.showCount is not a function, which is different from Java
問題:爲什麼不是mouse.showCount()
的功能?
因爲你把它放在** **動物對象上,而不是全部**動物對象。 – Tom 2015-04-04 14:35:48