TypeScript Object create()

In this chapter, we will explore the Object.create() method in TypeScript. This method creates a new object using an existing object as the prototype of the newly created object. Understanding how to use Object.create() is useful for creating objects with a specific prototype.

Table of Contents

  1. Definition
  2. Syntax
  3. Examples
  4. Conclusion

1. Definition

The Object.create() method creates a new object with the specified prototype object and properties. This is useful for creating objects that inherit properties and methods from another object.

2. Syntax

Object.create(proto, propertiesObject?);

Parameters

  • proto: The object to be used as the prototype for the newly created object.
  • propertiesObject (optional): An object specifying properties to be added to the newly created object.

Return Value

The method returns a new object with the specified prototype object and properties.

3. Examples

Let's look at some examples to understand how Object.create() works in TypeScript.

Example 1: Basic Usage

This example shows how to create a new object with a specified prototype.

let person = {
  greet: function() {
    console.log(`Hello, my name is ${this.name}`);
  }
};

let ravi = Object.create(person);
ravi.name = "Ravi";
ravi.greet(); // Output: Hello, my name is Ravi

Example 2: Adding Properties

This example shows how to create a new object with a specified prototype and additional properties.

let person = {
  greet: function() {
    console.log(`Hello, my name is ${this.name}`);
  }
};

let ravi = Object.create(person, {
  name: {
    value: "Ravi",
    writable: true,
    enumerable: true,
    configurable: true
  },
  age: {
    value: 25,
    writable: true,
    enumerable: true,
    configurable: true
  }
});

ravi.greet(); // Output: Hello, my name is Ravi
console.log(ravi.age); // Output: 25

Example 3: Inheriting Methods

This example shows how the newly created object inherits methods from the prototype.

let animal = {
  speak: function() {
    console.log(`${this.name} makes a noise`);
  }
};

let dog = Object.create(animal, {
  name: {
    value: "Dog",
    writable: true,
    enumerable: true,
    configurable: true
  }
});

dog.speak(); // Output: Dog makes a noise

Example 4: Creating a Nested Object

This example shows how to create a nested object with a specified prototype.

let address = {
  getCity: function() {
    return this.city;
  }
};

let person = Object.create(address, {
  name: {
    value: "Ravi",
    writable: true,
    enumerable: true,
    configurable: true
  },
  city: {
    value: "Mumbai",
    writable: true,
    enumerable: true,
    configurable: true
  }
});

console.log(person.getCity()); // Output: Mumbai

Example 5: Checking the Prototype

This example shows how to check the prototype of the newly created object.

let person = {
  greet: function() {
    console.log(`Hello, my name is ${this.name}`);
  }
};

let ravi = Object.create(person);
ravi.name = "Ravi";

console.log(Object.getPrototypeOf(ravi) === person); // Output: true

4. Conclusion

In this chapter, we explored the Object.create() method in TypeScript, which is used to create a new object with a specified prototype object and properties. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use Object.create() is essential for creating objects with specific prototypes in TypeScript applications.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare