How to instantiate an instance in JavaScript

I want to make unique sets of cars that can be held by different people. Cars will have similar basic specifications, but some of their properties and methods will change.

The problem is that I cannot figure out how this should work. How do you handle or instantiate instances in 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); 

This causes this error for obvious reasons. I know this is not a constructor (see below).

 Uncaught TypeError: Ferrari is not a constructor 

What I'm looking for is something like this. Each individual Ferrari will have a different price and a higher price.

 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'); }; 

Fred Ferrari is an example of a Ferrari, which is an example of a car. The problem is that I can't think of a way to make a constructor a constructor. Is there a way to do this, or am I just doing it wrong?

Other notes:

I know that I could just make each type of car a static JSON-like object, and then make instances of this and add new unique values. However, I would like to be able to keep the car as a designer, so I can easily do more when I need to.

I clearly lack an understanding of OOP or JavaScript here, but it would be great if someone could point me in the right direction.

+7
javascript constructor oop instances
source share
2 answers

What you are looking for is a derived constructor and its associated prototype, sometimes called a subclass.

In old-fashioned ES5, it looks like this:

 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; 

How it works:

  • Ferrari is a constructor function that, when called, calls Car with this , referring to a new instance, as well as to Car arguments. Car does its thing by setting these properties in an instance. Then we continue with the Ferrari code, which takes the arguments passed in and (in the above) remembers them as properties.

  • We guarantee that the object that will be assigned to the new Ferrari instances (which is taken from Ferrari.prototype ) uses Car.prototype as its prototype object, so if you add things to Car.prototype , they will also be present on Ferrari .

  • We guarantee that the standard constructor property on Ferrari.prototype applies to Ferrari .

Rather nicer in ES2015 (which you can use today through transpilation, for example, using tools such as 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; } } 
+5
source share

If you want Car instances to be constructors, Car must return a constructor.

The problem is that it will then inherit from Function.prototype instead of Car.prototype . If this is not desired, use Object.setProtetypeOf to fix this:

 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); 

Alternatively, you can add a method that will be used to β€œinstantiate” your instances.

 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); 
-one
source share

All Articles