• Mail us
  • Book a Meeting
  • Call us
  • Chat with us

NodeJS

Understanding Prototypes in JavaScript: A Comprehensive Guide


Handling Huge JSON files in Node.js

JavaScript is a prototypal language which means objects can inherit from other objects with a mechanism called prototypal inheritance. Unlike classical inheritance in languages such as Java or C++, this may seem less powerful at first, but makes sense once your mental model clicks.

What is a Prototype?

All JavaScript objects have a hidden [[Prototype]] property (instead of proto in older browsers). That prototype is just another object which the original object inherits method and properties from. If you attempt to access a property or method on an object which you do not own, JavaScript will look at to see if the property exists on the prototype of that object. If it isn’t, it will keep looking up the prototype’s prototype,, and so on, up the prototype chain.

Example: Prototypes in Action

For the sake of an example, let’s think about a simple one:

// Creating a person objectfunction Person(name, age) {  this.name = name;  this.age = age;}// Adding a method to the prototype of PersonPerson.prototype.sayHello = function() {  console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);};const person1 = new Person('Alice', 25);person1.sayHello(); // Outputs: "Hello, my name is Alice and I am 25 years old."

Here we have a constructor function Person, which sets name and age in this example: Then we add a method sayHello to the prototype of Person. When we create a new Person object like person1, it does not contain its own sayHello method, however we are still able to access it through the prototype chain.

Prototypal Inheritance

JavaScript allows objects to inherit from other objects via prototypes. That lets us share things across instances without repeating code. But for example, based on the Person, let us create a Student object:

function Student(name, age, grade) {  Person.call(this, name, age);  // Inherit properties from Person  this.grade = grade;}// Inheriting methods from PersonStudent.prototype = Object.create(Person.prototype);// Adding a new method to Student prototypeStudent.prototype.getGrade = function() {  console.log(`${this.name} is in grade ${this.grade}.`);};const student1 = new Student('Bob', 20, 'A');student1.sayHello(); // Outputs: "Hello, my name is Bob and I am 20 years old."student1.getGrade(); // Outputs: "Bob is in grade A."

Here we make the sayHello method available on the Student prototype through inheritance from Person, and we add another method, getGrade, directly on the Student prototype.

Prototype Chain

By using the prototype chain, JavaScript objects obtain properties and methods from other objects. JavaScript checks for a property on the object first when you attempt to access a property or method on an object. If it doesn’t, JavaScript checks to see if it exists on the object’s prototype, and if not, continues checking up the prototype chain until it finds the property or reaches the end of the chain (i.e., null). For example:

console.log(student1.hasOwnProperty('name'));  // true, name is an own property of student1console.log(student1.hasOwnProperty('sayHello'));  // false, sayHello is inherited from Person's prototype

 

Ready to transform your business with our technology solutions?   Contact Us today to Leverage Our NodeJS Expertise.

Share

facebook
LinkedIn
Twitter
Mail
NodeJS

Related Center Of Excellence