Object.create – Object Creation in JavaScript P6 – FunFunFunction #57




The Object.create() method in JavaScript creates a new object with the specified prototype object and properties. I walk through what it is, why Object.create exists in JavaScript, and how to use Object.create.

⏯ Highlights
00:20 What is Object.create in JavaScript
06:32 Why does JavaScript have Object.create
09:54 Lunch walk with musing on focus and side projects
12:44 How to use Object.create in JavaScript
15:33 Object.create and initializers (instead of constructors)

πŸ”— mpj on Twitter

πŸ”— mpj on Whale 🐳 πŸ‹ (iOS only as of Nov 2016)
https://askwhale.com/add/mpjme

πŸ”— Full series playlist (Object creation in JavaScript)

πŸ”— MPJ’s Musings playlist

πŸ”— Music: Peacock by 7 minutes dead

πŸ”— Music: Airglow by Laszlo

πŸ”— FIGHT by Tokyo Machine


Original source


48 responses to “Object.create – Object Creation in JavaScript P6 – FunFunFunction #57”

  1. Hi MPJ, is the below a good pattern to use?

    const model = (id) => {

    this._id = id;

    return Object.defineProperties({}, {
    "id": {
    get: () => {
    return this._id;
    },
    set: (value) => {
    if (typeof value !== 'string') {
    throw new Error("ID provided is not valid");
    }

    this._id = value;
    },
    enumerable: true
    }
    });
    };

    const scan = Object.create(model(id));

    It provides the functionality I want but if I log scan, I see

    Object {
    id: (. . .)
    }

    Seems I can't view the object without invoking the getters for each property. Do you know a way around this? I'm wanting to use this pattern as a model but I need to be able to return the object.

  2. goddammit mpj ! allllll the way down to say "forget about prototypes" ?iii know the reason u did this and i accept that! but if you didnt make ur videos as fun as it is right now ! i would curse you for the rest of my life πŸ˜€

  3. why do everything in js like setPrototype, isPrototypeOf tempt to say that it deals with prototype, but in reality, after setprototype function we get obj.prototyp is undefined, instead we have obj.__proto__ is value that we used within setprototype function. did I miss something?

  4. I could tell you were Swedish from the way you pronounce "just" πŸ™‚

    Great channel, love your videos! Will be promoting them amongst my junior colleagues.

  5. Isn't Object.create() much slower than prototype? You suggested that unless there is a solid reason, don't use prototype. If inheritance has to be used for performance reason, then prototype inheritance should be used instead of Object.create, right?

  6. Annnnd @ 10:48 starts why I never finish anything too. I had a business before, and when people tell me, "you should do a business around that" they don't really know what a business encompasses.

  7. Object.create() seems like something that should have just been in the language from day 1, and it deserves it's own special syntax. Instead, we have the new operator and the .prototype field, which is a total hack to make things look more like Java.

  8. I'm not sure I understand how returning this in the init function allows you to chain methods together… If Object.create returns a new object with the specified prototype as its new prototype, shouldn't you just be able to chain the init call onto the Object.create call regardless of what init returns?

  9. I've been programming for nearly a decade. It is extremely rare to find tutorials that are actually this entertaining. I sat through several episodes on subjects I already knew simply for the entertainment factor, as though I were watching any other youtuber I follow. You sir, are very much appreciated. Thank you.

  10. @15:30 you mentioned Performance is why we use Prototype, what's that means?Is it because that Object.create is a function already built so we don't need to write our own Objectcreate in browser or what?
    It's hard to tell which "performance" is good for a newbie >///< and thanks for this great series. <3

  11. Hi!

    Great episode once again, I've been watching some of your episodes for a while now and always love your content.

    Quick note, you chained the wrong methods at the end. Sure you can chain the construction of the object and the first method call which you did. But I think you meant to chain init() and makeSound(), otherwise the return this statement does not do an awful lot for the example.

    But besides that great stuff, thanks!

  12. Hi mpj! you should also explain this example)
    <pre>
    function Person(name, age){
    this.name = name;
    this.age = age;
    }

    Person.prototype.sayHi = function(){
    console.log('Hi my name is '+ this.name);
    }

    const john = new Person('John', 28);
    john.sayHi();

    console.log(Person.isPrototypeOf(john)); //false
    console.log(john instanceof Person); //true
    </pre>

  13. Wow man! I probably watched like 15 tutorials on your channel so far. And this whole time I was thinking you are living in Bay area California. The lunch break was a good addition. I really liked it!

  14. Earlier, it was hard for me to understand what is object creation and how and when we use it.. and your videos were like 20mins .whooopp.. i was like whoooppp..GOD!! but i really enjoyed it.. its funny and cute when ur trying to explain something and u also got confused wt you are saying.. ANYWAYS main thing is Your videos were quite amazing and intersting and now i get it. and i really love your videos now… THANK YOU ..!!!!

Leave a Reply