Learn How to create an Object in Javascript using Object.create() method for Beginners

Javascript Object is a function used for creating new objects with empty or existing properties and member functions.
When we create objects using Object.create() properties and prototypes of the parent object get passed or inherited to new assigned objects.

Syntax

Object.create();

Table of Contents

Creating new object using Object.create()

var person = {
    "first_name" : "Rohan",
    "last_name" : "Patil",
    "full_name" : function(){
        return this.first_name+" "+this.last_name;
    }
}

var student = Object.create(person);
console.log(student);

//Console Output
{}
Creating Javascript Object using Object.create() method
Creating Javascript Object using Object.create() method

In the above image, we can see that under __proto__ shows all the properties and member functions defined by us.

To access a property of an object student.

console.log(student.first_name); 
//Console Output
"Rohan"

console.log(student["first_name"]); 
//Console Output
"Rohan"

To access the member function of the object.

console.log(student.full_name()); 
//Console Output
"Rohan Patil"

Creating objects using null

We have come to know that to create objects we must pass some data inside curly braces with key and value pairs or else we can pass empty curly braces. This could still create an object for us to inherit its default properties and prototypes.

We can also create objects passing null as a parameter but like other normal objects this null. The object will be quite different. They don’t inherit prototypes from function Object.

When we create a new object it has some default or inherits some prototypes from Object such as toString(), valueOf() etc which a null object does not have.

Let us learn from example.

var normal_object = Object.create({});
var null_object = Object.create(null);

console.log(normal_object); // {}
console.log(null_object); // {}

// adding properties
normal_object.name = "Sunil";
null_object.name = "Sanjay";

console.log(normal_object); //{name: "Sunil"}
console.log(null_object); //{name: "Sanjay"}

// adding function to normal_object
normal_object.name_to_uppercase = function(){
    return this.name.toUpperCase();
}

normal_object.name_to_uppercase() // "SUNIL"

// adding function to null_object
null_object.name_to_uppercase = function(){
    return this.name.toUpperCase();
}

null_object.name_to_uppercase() // "SANJAY"

// now we'll call inherited toString() function on objects
normal_object.toString(); //"[object Object]"

null_object.toString(); //VM891:1 Uncaught TypeError: null_object.toString is not a function

When we call null_object.toString() this will raise an error, not a function. We must be very careful in creating objects with null as it may affect the normal execution of the program.

Conclusion

So it’s time to say bye we have come to the end of our post on Learn How to create an Object in Javascript using Object.create() method for Beginners. If you like our content then please do share it and comment to clarify your doubts.

Related Posts

Summary
Review Date
Reviewed Item
Learn How to create an Object in Javascript using Object.create() method for Beginners
Author Rating
51star1star1star1star1star
Software Name
Javascript
Software Name
Windows Os, Mac Os, Ubuntu Os
Software Category
Web Development