🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
- Definition
- Syntax
- Examples
- 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment