JS原型,用新功能创建对象

vuv*_*uvu 1 javascript constructor prototype

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

//框构造函数

function Box(x, y, w, h) {
  this.x = x;
  this.y = y;
  this.w= w;
  this.h= h;    
}
Run Code Online (Sandbox Code Playgroud)

//图像框功能:

 this.src = ....
 this.title = ...
Run Code Online (Sandbox Code Playgroud)

//文本框功能:

  this.font = ...
  this.font-size=...
  this.color=....
Run Code Online (Sandbox Code Playgroud)

小智 5

function Box(x, y, w, h) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;    
}
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)