Object Prototypes in JavaScript
Prototypes are a core concept in JavaScript, especially when it comes to object-oriented programming. This article aims to provide a clear understanding of object prototypes and how to use them in JavaScript.
What are Object Prototypes?
In JavaScript, every object has a prototype. The prototype is also an object, and all objects inherit properties and methods from their prototype.
How Do Object Prototypes Work?
Objects in JavaScript are linked to a prototype object. When you try to access a property that does not exist in an object, JavaScript tries to find this property in the prototype of this object.
Here's a simple example of an object prototype:
function Car(make, model) {
this.make = make
this.model = model
}
Car.prototype.startEngine = function () {
console.log('Engine started')
}
const myCar = new Car('Toyota', 'Corolla')
myCar.startEngine() // Logs: Engine started
In the above example, startEngine
is a method on the Car
prototype. All instances of Car
will have access to this method.
Why Use Object Prototypes?
Object prototypes are used for inheritance in JavaScript. They allow you to define methods and properties that are common to all instances of a particular object. This can lead to more efficient code, as shared properties and methods are defined only once on the prototype, rather than being duplicated in each instance.
Practical Examples of Object Prototypes
Here's an example of object prototypes used for inheritance:
function Vehicle(type) {
this.type = type
}
Vehicle.prototype.startEngine = function () {
console.log('Engine started')
}
function Car(make, model) {
Vehicle.call(this, 'Car')
this.make = make
this.model = model
}
Car.prototype = Object.create(Vehicle.prototype)
Car.prototype.constructor = Car
const myCar = new Car('Toyota', 'Corolla')
myCar.startEngine() // Logs: Engine started
In this example, Car
inherits from Vehicle
by setting its prototype to a new instance of Vehicle
. The startEngine
method is then available to all instances of Car
.
Let's break down the code and explain it in more detail:
-
Vehicle.call(this, 'Car');
- Here, we're using thecall
method. Thecall
method is a way to invoke a function directly by passing in thethis
context and arguments for the function. In this case, we're calling theVehicle
function with the context of the currentCar
instance (this
), and passing'Car'
as an argument. This means that when a newCar
is created, it will also run theVehicle
constructor function, setting thetype
property on theCar
. -
Car.prototype = Object.create(Vehicle.prototype);
- This line is setting up inheritance by creating a new object that inherits fromVehicle.prototype
, and assigning it toCar.prototype
. This means that all instances ofCar
will have access to the properties and methods defined onVehicle.prototype
. -
Car.prototype.constructor = Car;
- When a function is created in JavaScript, it automatically gets aprototype
object. Thisprototype
object has aconstructor
property that points back to the function. When we replaceCar.prototype
with a new object (as we did in the previous line), we also overwrite theconstructor
property. This line is correcting that by manually settingCar.prototype.constructor
back toCar
.
By doing this, we ensure that Car
instances have access to Vehicle
's methods (like startEngine
), and that the constructor
property of a Car
instance points to Car
, not Vehicle
.
Understanding object prototypes can help you write more efficient and organized JavaScript code.