[問題] JavaScript的繼承問題
程式碼是在這裡看到的
http://openhome.cc/Gossip/JavaScript/ClassSimulation.html
function Circle(x, y, r) {
// 設定物件各自狀態
this.x = x;
this.y = y;
this.r = r;
}
.
.
.
function Cylinder(x, y, r, h) {
Circle.call(this, x, y, r); // 呼叫父建構式
this.h = h;
}
// 原型繼承
Cylinder.prototype = new Circle();
// 設定原型物件之constructor為目前建構式
Cylinder.prototype.constructor = Cylinder;
// 以下在new時會再建構,不需要留在原型物件上
delete Cylinder.prototype.x;
delete Cylinder.prototype.y;
delete Cylinder.prototype.r;
我想請問最後3行為什麼要delete掉?
是因為this.x = x這裡就會new出來了嗎?
如果沒有delete掉會浪費記憶體?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 1.172.87.42
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):