1

我刚开始使用 JS,并为一个盒子创建了一个构造函数。现在我想要一个可以存储图像的盒子和一个可以存储文本的盒子。如何使用原型声明新的盒子对象?

//盒子构造函数

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=....
4

1 回答 1

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);
于 2013-04-06T22:42:27.103 回答