Difference Between Objects and Prototypes in JavaScript
Last Updated :
11 Dec, 2023
Objects in JavaScript
The Objects in JavaScript are instances of the class or constructors and they can hold properties and methods. These properties and methods can be unique to the object or inherited from the prototypes. The Objects can be created using the constructor functions, object literals, or Object constructors.
Syntax:
const person = {
name: 'Kumar',
age: 20,
greet: function() {
// code
}
};
Example: In this Example, a car object might have properties like "make" and "model" and methods for starting the engine or changing gears.
JavaScript
car1 = { make: 'Toyota', model: 'Camry' };
// Adding a method to object
car1.start = function () {
console.log('Engine started');
};
// Using the object and its method
car1.start();
Prototypes in JavaScript
The Prototypes in JavaScript are template objects that provide shared properties and methods to the other objects. Every object has a built-in property that is called its prototype. and prototype is an object itself so it also has its own prototype. They are not created explicitly as prototypes but exist as a blueprint for the objects of the specific type. The Prototypes facilitate inheritance by allowing the objects to inherit their properties and methods.
Syntax:
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.greet = function() {
// code
}
Example: In this example, a Cars constructor function is defined to create car objects with make and model properties, and a shared start method is added to the Cars prototype for initiating the engine.
JavaScript
function Cars(make, model) {
this.make = make;
this.model = model;
}
// Adding a shared method to Car prototype
Cars.prototype.start = function () {
console.log('Engine started');
};
// Creating car objects
const myCars = new Cars('Toyota', 'Camry');
const anotherCars = new Cars('TATA', 'Accord');
// Using the shared method from prototype
myCars.start();
anotherCars.start();
OutputEngine started
Engine started
Difference Between Objects and Prototypes in JavaScript
|
The Objects are instances of the classes or constructors and they can hold properties and methods.
| The Prototypes are template objects that provide the shared properties and methods to other objects.
|
Created using the constructor functions, object literals or Object constructor.
| Not created explicitly as prototypes but exist as a blueprint for the objects.
|
The Objects have their own unique properties and can inherit from the prototypes.
| The Prototypes define shared properties that can be inherited by the objects.
|
The Objects can have their own methods, distinct from the methods in the prototypes.
| Prototypes can define methods that are shared the among objects of same type.
|
The Objects can inherit from the other objects using the mechanisms like Object.create() or constructors.
| Prototypes facilitate inheritance by allowing the objects to inherit their properties and methods.
|
Similar Reads
Difference Between Variables and Objects in JavaScript The variables and objects are fundamental concepts but they serve different purposes. The Variables are used to store data values while objects are used to group related data and functions into a single entity. JavaScript VariableA variable in JavaScript is a named container that stores a value. It
2 min read
Difference between Array and Array of Objects in JavaScript ArrayAn Array is a collection of data and a data structure that is stored in a sequence of memory locations. One can access the elements of an array by calling the index number such as 0, 1, 2, 3, ..., etc. The array can store data types like Integer, Float, String, and Boolean all the primitive dat
3 min read
Difference Between JavaScript Arrays and Objects Below are the main differences between a JavaScript Array and Object.FeatureJavaScript ArraysJavaScript ObjectsIndex TypeNumeric indexes (0, 1, 2, ...)Named keys (strings or symbols)OrderOrdered collectionUnordered collectionUse CaseStoring lists, sequences, ordered dataStoring data with key-value p
1 min read
Difference Between for...in and Object.keys() in JavaScript The for...in and Object.keys() in JavaScript are used to iterate over the properties of an object. While they might seem similar at first glance they have distinct usage, behavior, and characteristics. This article will explore these differences in detail.These are the following topics that we are g
3 min read
Difference between proto and prototype In this article, we will be going to cover the topic what is proto and prototypes, their syntax, examples, and what are differences exist between both, and how they differ and how they differ in different aspects. Proto and prototype both are objects that help in whether creating an array, object,
3 min read
Difference between Object.keys() and Object.entries() methods in JavaScript Object.keys() and Object.entries() are methods in JavaScript used to iterate over the properties of an object. They differ in how they provide access to object properties: Object.keys() returns an array of a given object's own enumerable property names, while Object.entries() returns an array of a g
2 min read