![]() |
The new keyword and Object.create() are frequently used to instantiate objects in JavaScript but they have distinct characteristics and use cases. Understanding the differences between new and Object.create() is essential for mastering object creation in JavaScript. What is new?The new keyword in JavaScript is primarily used with the constructor functions to create new instances of the objects. When invoked new initializes a new object, sets its prototype binds this to the new object, and returns the object implicitly. Syntax:function MyClass(prop1, prop2) Example: This example shows the creation of an object using new keyword.
Output: Person { name: 'Kumar', age: 30 } What is Object.create()?The Object.create() is a method in JavaScript that creates a new object with the specified prototype object and properties. Unlike new, it allows more control over the prototype chain and enables prototypal inheritance without the constructor functions. Syntax:const newObj = Object.create(proto, [propertiesObject]); Example: This example shows the ceation of an object using Object.create() function.
Output: Hello, my name is Kumar Difference Between new and Object.create
|
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 17 |