Tech Junkie Blog - Real World Tutorials, Happy Coding!: JavaScript Objects Deep Dive : Create Objects With Object.Create

Monday, November 1, 2021

JavaScript Objects Deep Dive : Create Objects With Object.Create

Object.Create( ) is a static function which can take two arguments.  The first argument is the prototype of the object, while the second argument is the properties of the argument.  A prototype is a second object that is created with every JavaScript object.  It is like a blueprint, or the model that the object is built on.  Think of it as a car prototype.

So let's create an object with Object.Create( ) function:

var person = Object.create({
    name: "Tech Junkie",
    location: "Mars",
    hobbie: "Video Games"
});

console.log(person);


Here is how the object looks like:
















Notice how it's different than creating an object with new keyword.  In this case you are actually using the object's prototype object.

Here is how the object would look with the new keyword

var person = new Object({
    name: "Tech Junkie",
    location: "Mars",
    hobbie: "Video Games"
});

console.log(person);













1 comment:

Search This Blog