我刚开始使用JS,我为一个盒子创建了一个Contructor。现在我想要一个可以存储图像的盒子和一个可以存储文本的盒子。如何使用原型声明新的盒子对象?
// box constructor
function Box(x, y, w, h) {
this.x = x;
this.y = y;
this.w= w;
this.h= h;
}
//图片框功能:
this.src = ....
this.title = ...
//文本框功能:
this.font = ...
this.font-size=...
this.color=....
答案 0 :(得分:5)
function Box(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
function ImageBox(x, y, w, h) {
Box.call(this, x, y, w, h); // Apply Box function logic to new ImageBox object
this.src = ....
this.title = ...
}
// Make all ImageBox object inherit from an instance of Box
ImageBox.prototype = Object.create(Box.prototype);
function TextBox(x, y, w, h) {
Box.call(this, x, y, w, h); // Apply Box function logic to new TextBox object
this.font = ...
this.font-size =...
this.color =....
}
// Make all TextBox object inherit from an instance of Box
TextBox.prototype = Object.create(Box.prototype);