Skip to main content

Python type() Function

When learning Python, you will work with different types of data, such as numbers, text, decimal values, and Boolean values. Python provides several built-in functions to help you work with these data types. One of the most useful functions is the type() function . The type() function tells you the data type of a value or variable. It helps you understand what kind of data Python is working with and can make debugging your programs easier. For example, consider these variables: age = 20 price = 99.50 name = "Rahul" is_student = True Although all four variables store data, their data types are different. Python allows us to check these types using type() . Let's understand how it works with simple examples. 1. What Is the type() Function in Python? The type() function is a built-in Python function that returns information about the type of an object. In beginner-level programming, you can use it to check whether a value is an integer, a floating-point number, a string,...

JavaScript Prototypes and Prototype Chain: Mastering Inheritance in JavaScript

One of the most unique features of JavaScript is its inheritance model. Unlike many programming languages such as Java or C++, JavaScript does not use classical inheritance by default. Instead, it uses Prototype-Based Inheritance.

Almost every object in JavaScript is connected to another object called its prototype. This hidden connection allows objects to inherit properties and methods without copying them.

Understanding prototypes is essential because they are used internally by:

  • Arrays

  • Objects

  • Functions

  • Classes

  • DOM Elements

  • Built-in JavaScript Objects

In this article, we will learn:

  • What a Prototype is

  • Prototype-Based Inheritance

  • Prototype Chain

  • Constructor Functions

  • Prototype Methods

  • Object.create()

  • ES6 Classes and Prototypes

  • Practical Examples

  • Interview Questions


What is a Prototype?

A prototype is another object from which an object inherits properties and methods.

Think of a prototype as a template that provides common functionality to multiple objects.

Example:

let person =
{
    greet()
    {
        console.log("Hello");
    }
};

let student =
Object.create(person);

student.greet();

Output:

Hello

The student object does not contain the greet() method itself. It inherits it from its prototype.


Why Do We Need Prototypes?

Imagine creating 1,000 student objects.

Without prototypes:

Each Object

↓

Own Copy of greet()

↓

More Memory Used

With prototypes:

All Objects

↓

Share One greet()

↓

Less Memory Used

This makes JavaScript more memory-efficient.


Every Object Has a Prototype

Example:

let car =
{
    brand: "Toyota"
};

Internally:

car

↓

Prototype

↓

Object.prototype

Every normal object ultimately inherits from Object.prototype.


The Prototype Chain

When JavaScript looks for a property, it searches in the following order:

  1. Current Object

  2. Prototype

  3. Prototype's Prototype

  4. Until null

This sequence is called the Prototype Chain.


Prototype Chain Diagram

student

↓

person

↓

Object.prototype

↓

null

Example

let person =
{
    country: "India"
};

let student =
Object.create(person);

console.log(student.country);

Output:

India

JavaScript first searches student. Since country is not found, it searches person.


Property Lookup Process

Suppose we execute:

student.country;

JavaScript searches:

student

↓

person

↓

Object.prototype

↓

null

The search stops as soon as the property is found.


Constructor Functions

Before ES6 classes, constructor functions were used to create multiple objects.

Example:

function Student(name)
{
    this.name = name;
}

let s1 =
new Student("Rahul");

console.log(s1.name);

Output:

Rahul

Adding Methods to a Prototype

Instead of creating a method inside every object:

function Student(name)
{
    this.name = name;
}

Student.prototype.sayHello =
function()
{
    console.log(
    "Hello " + this.name
    );
};

let s1 =
new Student("Rahul");

s1.sayHello();

Output:

Hello Rahul

The method is shared by all Student objects.


Why Use Prototype Methods?

Without prototypes:

1,000 Objects

↓

1,000 Copies

↓

High Memory Usage

With prototypes:

1,000 Objects

↓

1 Shared Method

↓

Efficient Memory Usage

Object.create()

Object.create() creates a new object using another object as its prototype.

Example:

let animal =
{
    eat()
    {
        console.log("Eating...");
    }
};

let dog =
Object.create(animal);

dog.eat();

Output:

Eating...

Overriding Properties

Example:

let person =
{
    city: "Kolkata"
};

let student =
Object.create(person);

student.city = "Delhi";

console.log(student.city);

Output:

Delhi

The object's own property overrides the prototype property.


Prototype vs Own Property

Example:

let person =
{
    country: "India"
};

let student =
Object.create(person);

student.name = "Rahul";

Here:

student.name

↓

Own Property

while:

student.country

↓

Inherited Property

Checking Own Properties

JavaScript provides:

hasOwnProperty()

Example:

console.log(
student.hasOwnProperty("name")
);

Output:

true

Example:

console.log(
student.hasOwnProperty("country")
);

Output:

false

Built-in Prototype Example

Arrays inherit from Array.prototype.

Example:

let numbers =
[1,2,3];

console.log(
numbers.push
);

The push() method comes from Array.prototype.


Function Prototype

Functions also have prototypes.

Example:

function test()
{
}

console.log(
test.prototype
);

Output:

Prototype Object

ES6 Classes and Prototypes

Many developers think classes replace prototypes.

Actually:

ES6 Classes

↓

Use Prototypes Internally

Example:

class Student
{
    constructor(name)
    {
        this.name = name;
    }

    greet()
    {
        console.log(
        "Hello " + this.name
        );
    }
}

let s1 =
new Student("Rahul");

s1.greet();

Output:

Hello Rahul

The greet() method is stored on Student.prototype.


Prototype Chain with Classes

s1

↓

Student.prototype

↓

Object.prototype

↓

null

Real-World Example

Imagine a game.

Every player has:

  • Move

  • Jump

  • Attack

Instead of storing these methods inside every player:

Player Object

↓

Player Prototype

↓

Shared Methods

This saves memory and improves performance.


Common Mistakes

Adding Methods Inside Constructor

Wrong:

function Student(name)
{
    this.name = name;

    this.greet =
    function()
    {
        console.log("Hello");
    };
}

Every object gets its own copy.


Correct Approach

Student.prototype.greet =
function()
{
    console.log("Hello");
};

One shared method.


Practical Example

function Car(brand)
{
    this.brand = brand;
}

Car.prototype.show =
function()
{
    console.log(
    this.brand
    );
};

let car1 =
new Car("Toyota");

let car2 =
new Car("Honda");

car1.show();

car2.show();

Output:

Toyota

Honda

Prototype Chain Summary

Current Object

↓

Prototype

↓

Object.prototype

↓

null

JavaScript searches in this order whenever a property or method is accessed.


Advantages of Prototypes

  • Memory Efficient

  • Supports Inheritance

  • Faster Object Creation

  • Shared Methods

  • Reusable Code


Interview Questions

What is a Prototype?

A prototype is an object from which another object inherits properties and methods.


What is the Prototype Chain?

The sequence JavaScript follows while searching for properties.


Why are Prototype Methods Better?

Because all objects share the same method instead of creating multiple copies.


What is Object.create()?

A method used to create a new object with a specified prototype.


Do ES6 Classes Use Prototypes?

Yes. ES6 classes are built on top of JavaScript's prototype system.


Best Practices

  • Store shared methods on the prototype.

  • Use Object.create() for custom inheritance.

  • Prefer ES6 classes for cleaner syntax while understanding that they use prototypes internally.

  • Avoid duplicating methods inside constructors.

  • Learn the prototype chain for debugging inheritance issues.


Real-World Applications

Prototype-based inheritance is used in:

  • React Components (internally through JavaScript objects)

  • Node.js Applications

  • Browser APIs

  • Game Development

  • Object-Oriented JavaScript Libraries

  • Large Enterprise Applications

Understanding prototypes helps developers write efficient, reusable, and scalable JavaScript code.


Conclusion

Prototypes are the foundation of JavaScript's inheritance model. Every object is connected to another object through the Prototype Chain, allowing JavaScript to share methods and properties efficiently.

Comments

Popular posts from this blog

Understanding Comments in Java: A Beginner’s Guide

When learning Java programming, one of the first concepts you’ll come across is comments . Comments may not affect how a program runs, but they play a crucial role in making code more understandable, readable, and maintainable. Let’s dive deeper into what comments are, why they’re important, and the different types of comments in Java. What are Comments in Java? A comment in Java is a piece of text written within a program that is ignored by the compiler . In other words, comments are notes for humans, not instructions for machines. They help developers explain the purpose of code, describe logic, or temporarily disable certain lines of code without deleting them. Why are Comments Important? 📌 Readability – Comments make code easier to understand for you and others. 📌 Maintenance – When you revisit your code after months, comments remind you what you were doing. 📌 Collaboration – In team projects, comments explain code to fellow developers. 📌 Debugging – You c...

Java Program to Check Voting Eligibility

In this program, we’ll take the user’s age as input and check whether the person is eligible to vote or not. In India, the minimum age required to vote is 18 years . 💻 Program Code // Java Program to Check Voting Eligibility import java.util.Scanner; public class VotingEligibility { public static void main(String[] args) { // Create Scanner object to take input from user Scanner sc = new Scanner(System.in); // Ask user to enter age System.out.print("Enter your age: "); int age = sc.nextInt(); // Check eligibility if (age >= 18) { System.out.println("You are eligible to vote."); } else { System.out.println("You are not eligible to vote yet."); System.out.println("You can vote after " + (18 - age) + " year(s)."); } sc.close(); // Close the scanner } } 🧠 Explanation import java.util.Scanner; → Imp...

Java Program to Calculate Total Marks and Average of Five Subjects

💡 Program Explanation In this program: The user will input marks of five subjects . The program will calculate the total marks and average . It will then display both results clearly. 🧑‍💻 Java Code: // Java Program to Calculate Total Marks and Average of Five Subjects import java.util.Scanner; // Importing Scanner class for user input public class TotalAndAverage { public static void main(String[] args) { // Creating Scanner object to take input from user Scanner sc = new Scanner(System.in); // Declaring variables for five subjects double subject1, subject2, subject3, subject4, subject5; // Taking marks input for each subject System.out.print("Enter marks of Subject 1: "); subject1 = sc.nextDouble(); System.out.print("Enter marks of Subject 2: "); subject2 = sc.nextDouble(); System.out.print("Enter marks of Subject 3: "); subject3 = sc.nextDo...