Use Polymorphism with Prototype Linked Objects

Tyler Clark
InstructorTyler Clark
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 6 years ago

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.

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.

egghead
egghead

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today