JavaScript Basics: Object.create

in #javascript6 years ago

In an earlier post (Object Creation and Manipulation Functions in JavaScript), I listed some functions that are available on the Object constructor. Here is the list once again:

    create
    assign
    defineProperty
    defineProperties
    getPrototypeOf
    setPrototypeOf
    getOwnPropertyDescriptor
    getOwnPropertyDescriptors
    getOwnPropertyNames
    getOwnPropertySymbols
    is
    seal
    freeze
    preventExtensions
    isExtensible
    isSealed
    isFrozen
    keys
    entries
    values

And also these methods are on Object.prototype:

    hasOwnProperty
    isPrototypeOf
    propertyIsEnumerable

These functions are used for object creation and manipulation. In the current post, I am going to talk about the first one of these functions, Object.create.

Every constructor function in JavaScript has an associated prototype that is used as the prototype for the instances created by that constructor. In fact, the inheritance pattern in JavaScript is based on prototypes, so you can just create an object based on a prototype, without the need to define a constructor function. That's when Object.create becomes useful.

So Object.create creates an object based on a prototype. The prototype is specified as the first argument to this function. You can also specify a second argument that is a map of property names and property descriptors.

Here are some examples:

    let prototype = {
        name: "Ali",
        greet() {
            console.log("Hi. My name is " + this.name);
        },
    };
    
    let person1 = Object.create(prototype);
    
    let person2 = Object.create(prototype, {
        age: {
            value: 19,
            writable: true,
            enumerable: true,
            configurable: true,
        },
    });

As you see in the example, the second argument follows the same pattern used in Object.defineProperties.


Related Posts

Sort:  

Very good explanation on Object.create method .I used to create object using the new operator like var obj=new Object(); other way is using the object literal notation like var obj={} and another way is to create a object from the function(i.e constructor).

Thanks. Sometimes it is easier to use Object.create.

I am making a game in ethereum. Though i know java programming a little, but i wrote that. Now i am at level 3. I am learning java from your post. It helps me a lot.

That's very nice. Just don't confuse Java and JavaScript. Although their syntax is similar, they are basically different languages.

Yes, it's really confusing. Learning slowly. Thanks for your advise sir.

Welcome to You Deserve https://steemit.com/trending/hive-183209

I hope that you will invite friends to help the novices in the community. Hand in Hand towards the Imam, all greetings

Your post is so helpful

i like you sir.i follow you sir.i like your post

nice post. great efforts.

Thanks sir @ghasemkiani for sharing programming post . Its very difficult to understand but i will.
Stay blessed

wow..thats so educative post...
great learing media..
thats nice.. .
carry on dear

technology is developing day by day
by this programing is easy today
thanks for the valuable post

This post is quiet useful for the creation of object and its manipulation.

Coin Marketplace

STEEM 0.21
TRX 0.14
JST 0.030
BTC 69500.15
ETH 3394.50
USDT 1.00
SBD 2.75