Tech Junkie Blog: JavaScript Objects Deep Dive : Create Objects With Object.Create

Friday, April 28, 2017

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:

  1. Hm, have never seen the syntax like this on picrelated before - is it some framework? To be frank, I'm quite unexperienced when it comes to javascript as well as for any other programming language at all. So, I've started my way through that with js 2 months ago, because you know, things here are not so complicated as they are with call-it the real languages. But nevertheless, my skills with the fundamentals are pretty nice so far, so I'm able to build some complex things like this very javascript snake game https://explainjava.com/snake-game/ and yep, there is some manuals on that to peeking if you stuck. I understand clearly that this is not the perfect limit, but it's the best option for myself to start with at least

    ReplyDelete