为什么在js中使用原型方法?当我为两者创建一个新对象时,他们会做同样的事情吗?
function Rectangle(w, h) {
this.width = w;
this.height = h;
this.area = function( ) { return this. width * this. height; }
}
Run Code Online (Sandbox Code Playgroud)
要么
Rectangle.prototype.area = function(){
return this.width * this.height
}
Run Code Online (Sandbox Code Playgroud) javascript ×1