【发布时间】:2012-01-16 00:50:49
【问题描述】:
好的,所以我对 JS 中的 OOP 的想法有些陌生。
下面写的这两个sn-ps代码有什么区别:
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; //不能像Person.instance_counter这样的类名访问(这是静态的)
标签: javascript prototype-programming