Check out a free preview of the full The Hard Parts of Object Oriented JavaScript course

The "Object.create" Lesson is part of the full, The Hard Parts of Object Oriented JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Will uses Object.create to start a conversation about the DRY principle.


Transcript from the "Object.create" Lesson

>> Will Sentance: There's one other interesting way, we're gonna see a bunch of ways for an object. But one other interesting one we're gonna see right now, that's gonna turn out to give us very fine-grained control later on over what our object might also have access to, very interesting. This approach is called object-dot-create, is a built in method, a built in function in JavaScript that always whenever we pass through it, we could pass it as its input an objects for the objects.

Whenever we pass through it, now, well, in this case is always gonna store on the left hand side an empty object. Let's do it, user 3 is therefore gonna be in memory an empty object, there it is. Into which we're gonna store our properties name, Eva, score 9, and again, our increment method.

So there it is, our user 3 object. We created it using this interesting built-in tool for creating objects object-dot-create. Very explicitly named. Whatever we pass to that always it returns out an empty object. I could pass in objects full of objects no problem, empty object, but what we pass in is gonna determine, we're gonna see, what bonus feature we might get access to, with user 3 if we pass in something more interesting than null.

All to come, but note folk. What have we done? We've started building our application. We have an object representing user 3, we have an object representing user2. Both of these objects have all the pertinent functionality that is applicable to Eva, applicable to Julia, but we're handcrafting these objects.

We're sticking properties in there manually one by one, and we don't have a bunch of developers sitting behind the scenes when you sign up to a site, or log in, and say, don't worry, don't worry, Terry's logged in. Get the object created. No, no, no, no. We have a function that will then allow us to call it when the user logs in, and fill out the object at that moment.

Whenever we have repetitive code, we do not want to be doing it from scratch every time. So we wrap this work in a function. And it turns out, people, this ends up being our first solution.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now