Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features
Autoplay

    Use Polymorphism with Prototype Linked Objects

    Tyler ClarkTyler Clark
    javascriptJavaScript

    Polymorphism is a powerful tool in many classical languages. However, it works a little differently in JavaScript and it’s important to understand the requirements of when a property can be overridden. Let’s explore these differences with some custom objects.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    Instructor: 00:00 To get started, let's do const foo equals an object with name of Tyler, const bar equals an object of last name of Clark. Then we'll do object.setPrototypeOf, bar and foo. Polymorphism or shadowing, is when an inherited property is redefined.

    00:19 Here, we have two objects, bar and foo. We're setting foo to be the next-in-line prototype of object bar. If we console.log bar.name, we get Tyler, even though name lives on foo, and not bar. Properties that live on the prototype chain can be redefined in all but two cases.

    00:40 If we commented out name inside the object and did object.definePropertyOf foo, called name, and give it a value of Tyler, but make it writable false, we can see down at the bottom that our console.log is not James anymore, but back to Tyler.

    00:59 When a property exists up the prototype chain, and that property is configured as writable false, you're not able to assign that property to down-the-chain objects. Even though that configuration does not exist on bar, we cannot set it. It stays and goes back to Tyler.

    01:18 If we were in strict mode, if we tried to reassign a property, we would get an error. Now, if we were to comment our defined property and make our name a setter, which means we turn it into a function that takes an argument of name.

    01:34 Let's just assign current name to name. Now, if we look back down to our console.log, we'll see that we get an undefined. If a property is a setter up the chain, then the setter will always be called. The property will not be added to our bar object, and the setter is not redefined.

    01:55 Since we're calling a setter by doing bar.name, we're going to get an undefined back. If we console.log just bar, we'd see that we get a property, current name of James. To recap, properties with the same key can be defined on a down-the-chain object in all but two cases.

    02:13 If that same name key is configured writable false, or it's a setter, it cannot be added. In strict mode, trying to assign a same name key that is configured writable false up the chain will throw an error.

    Discuss

    Discuss