我对 Java版原型的使用感到困惑不解。
举以下例子:
(1)
function Rectangle(w, h) { this.width=w; this.height=h; this.area=function() { this.width * this.height; } }
And a similar case where the area is attached to a prototype as follows:
(2)
function Rectangle(w, h) { this.width=w; this.height=h; } Rectangle.prototype.area=function() { this.width * this.height; }
- What is the diffrence between (1) and (2) ?
- When would you use (1) or (2) for writing methods on classes?