可能重複:
Understanding prototypal inheritance in JavaScript原型的目的是什麼?
OK,所以我有點新的JS在面向對象的思想。
什麼的代碼這兩個片段之間的區別下面寫:
function animal(){
this.name = 'rover';
this.set_name = function(name){
this.name = name;
}
}
function animal(){
this.name = 'rover';
}
animal.prototype.set_name = function(name){
this.name = name;
}
他們都做同樣的事情,所以有什麼區別?
實例級別共享成員(非靜態)的類。例如)Person.prototype.instance_counter = 0; //不能用ClassName像Person.instance_counter(這是靜態的)訪問。 –