繁体   English   中英

如何在JavaScript中创建实例的实例

[英]How do you create an instance of an instance in JavaScript

我想制作一组不同人可以容纳的汽车的独特实例。 汽车将有类似的基本规格,但他们的一些属性和方法会有所不同。

我遇到的问题是我无法弄清楚它应该如何工作。 您如何在JavaScript中处理或创建实例实例?

var Car = function(make, country) {
    this.make = make;
    this.country = country;
};

var Ferrari = new Car('Ferrari', 'Italy');

var fred = new Person() {};
var fred.cars['Ferrari'] = new Ferrari(1200, 300000);

出于显而易见的原因,这会导致此错误。 我知道它不是构造函数(见下文)。

Uncaught TypeError: Ferrari is not a constructor

我正在寻找的是这样的。 法拉利的每个不同实例都有不同的价格和价格。

var Ferrari = function(currentPrice, miles) }
    this.currentPrice = currentPrice;
    this.miles = miles;
    // this is an instance of car, aka it needs the result of this:
    // new Car('Ferrari', 'Italy');

};

弗雷德的法拉利是法拉利的一个例子,这是一个汽车的例子。 问题是我想不出一种方法来使构造函数构建一个构造函数。 有没有办法做到这一点,或者我只是以错误的方式解决这个问题?

其他说明:

我知道我基本上只需要使每种类型的汽车成为类似静态JSON的对象,然后创建它的实例并添加新的唯一值。 但是,我希望能够将Car作为构造函数,这样我就可以在需要时轻松制作。

我在这里显然缺少对OOP或JavaScript的一些理解,但如果有人能指出我正确的方向,那将会很棒。

您正在寻找的是派生构造函数和相关原型,有时称为子类

在老式的ES5中它看起来像这样:

var Car = function(make, country) {
    this.make = make;
    this.country = country;
};
var Ferrari = function(currentPrice, miles) {
    Car.call(this, "Ferrari", "Italy");
    this.currentPrice = currentPrice;
    this.miles = miles;
};
Ferrari.prototype = Object.create(Car.prototype);
Ferrari.prototype.constructor = Ferrari;

工作原理:

  • Ferrari是一个构造函数,调用它时,调用Carthis指的是新的实例,用参数一起Car需求。 Car会在实例上设置这些属性。 然后我们继续使用Ferrari的代码,该代码接受传入的参数并且(在上面)将它们记为属性。

  • 我们确保将由new Ferrari (取自Ferrari.prototype )分配给实例的对象使用Car.prototype作为其原型对象,这样如果您向Car.prototype添加内容,它们将出现在Ferrari也是。

  • 我们确保Ferrari.prototype上的标准constructor属性指的是Ferrari

在ES2015中更好(你今天可以通过翻译使用,例如像Babel这样的工具):

class Car {
    constructor(make, country) {
        this.make = make;
        this.country = country;
    }
}
class Ferrari extends Car {
    constructor(currentPrice, miles) {
        super("Ferrari", "Italy");
        this.currentPrice = currentPrice;
        this.miles = miles;
    }
}

如果您希望Car实例是构造函数, Car必须返回构造函数。

问题是,它将继承Function.prototype而不是Car.prototype 如果这是不合需要的,请使用Object.setProtetypeOf来修复它:

function Person() {
  this.cars = {};
}
function Car(make, country) {
  var instance = function(currentPrice, miles) {
    this.currentPrice = currentPrice;
    this.miles = miles;
  };
  instance.make = make;
  instance.country = country;
  Object.setPrototypeOf(instance, Car.prototype); // slow!!
  return instance;
}
var Ferrari = new Car('Ferrari', 'Italy');
var fred = new Person();
fred.cars.ferrari = new Ferrari(1200, 300000);

或者,您可以添加一个用于“实例化”实例的方法。

function Person() {
  this.cars = {};
}
function Car(make, country) {
  this.make = make;
  this.country = country;
}
Car.prototype.instantiate = function(currentPrice, miles) {
  var instance = Object.create(this);
  instance.currentPrice = currentPrice;
  instance.miles = miles;
  return instance;
};
var ferrari = new Car('Ferrari', 'Italy');
var fred = new Person();
fred.cars.ferrari = ferrari.instantiate(1200, 300000);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM