JavaScript Object create() - Create New Object

Updated on November 25, 2024
create() header image

Introduction

The Object.create() method in JavaScript is a powerful tool for creating new objects with a specified prototype object and properties. This method provides a straightforward way to inherit properties from another object, facilitating prototyping and helping to establish a proper prototype chain without invoking a constructor.

In this article, you will learn how to use the Object.create() method effectively. Discover how to create new objects while inheriting properties from another object and see practical examples that demonstrate its usage in real-world programming scenarios.

Understanding Object.create()

Basic Usage of Object.create()

  1. Start with understanding that Object.create() allows you to specify the prototype of the new object.

  2. Pass the prototype object as the first argument.

    javascript
    const person = {
        isHuman: true,
        printIntroduction: function() {
            console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
        }
    };
    
    const me = Object.create(person);
    me.name = 'Alice';  // "name" is a property set on "me", but not on "person"
    me.printIntroduction();
    

    This code snippet creates an object me that inherits from person. When me.printIntroduction() is called, it uses properties from both me and person.

Setting Properties at Creation Time

  1. Understand that properties can also be set directly during object creation using the second argument of Object.create().

  2. Pass an object that describes properties.

    javascript
    const person = {
        isHuman: true,
        printIntroduction: function() {
            console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
        }
    };
    
    const me = Object.create(person, {
        name: { value: 'Alice', writable: true, configurable: true, enumerable: true }
    });
    me.printIntroduction();
    

    Here, me is created with the name property already defined. This approach helps ensure that properties have the desired descriptor settings from the outset.

Advanced Techniques with Object.create()

Creating Immutable Objects

  1. Use Object.create() to make an immutable prototype.

  2. Combine this with property descriptors that disable writing and configuring.

    javascript
    const immutablePerson = Object.create(null, {
        name: { value: 'Bob', writable: false, configurable: false }
    });
    
    console.log(immutablePerson.name);  // Outputs: Bob
    // immutablePerson.name = 'Alice';  // This line would fail if uncommented, due to "writable: false"
    

    This code provides a way to create an object where the name property cannot be altered after creation.

Prototypal Inheritance without Constructors

  1. Understand that Object.create() is often used to inherit properties without calling a constructor.

  2. This method is particularly useful when setting up prototype chains in a more flexible and decoupled manner.

    javascript
    const developer = Object.create(person);
    developer.code = function() {
        console.log("coding...");
    };
    
    const devAlice = Object.create(developer);
    devAlice.name = 'Alice';
    devAlice.printIntroduction();
    devAlice.code();
    

    In this snippet, devAlice not only inherits from person but also from developer, showcasing multi-level inheritance without constructors.

Conclusion

The Object.create() method in JavaScript is a versatile function that simplifies object creation and prototype linkage. It offers a robust alternative to traditional constructor functions, especially when attempting to establish complex inheritance structures. By mastering Object.create(), you can effectively manage prototypal inheritance and property characteristics in your JavaScript applications, crafting more maintainable and scalable code.