Javascript 一次定义整个原型和单独设置方法之间的区别
简单地说,这两段代码之间有什么区别吗?有什么理由使用其中一段与另一段 代码#1: 代码#2:Javascript 一次定义整个原型和单独设置方法之间的区别,javascript,prototype,Javascript,Prototype,简单地说,这两段代码之间有什么区别吗?有什么理由使用其中一段与另一段 代码#1: 代码#2: 那是一样的。没有理由会有不同 但在我看来,最好使用第二种形式,因为如果您决定让东西“类”继承另一种形式,那么您需要更改的内容就更少了: thing.prototype = new SuperThing(); // inherits from the SuperThing class thing.prototype.some_method=function(){ alert('stuff');
那是一样的。没有理由会有不同 但在我看来,最好使用第二种形式,因为如果您决定让
东西
“类”继承另一种形式,那么您需要更改的内容就更少了:
thing.prototype = new SuperThing(); // inherits from the SuperThing class
thing.prototype.some_method=function(){
alert('stuff');
}
它还使在多个javascript文件中定义类变得更容易
为了更好地保持代码的连贯性,我更喜欢使用相同的结构,即thing.prototype.some_method=function(){
one
var thing=function(){}
thing.prototype.some_method=function(){
alert('stuff');
}
thing.prototype = new SuperThing(); // inherits from the SuperThing class
thing.prototype.some_method=function(){
alert('stuff');
}