Skip to content

08. Prototypes and Inheritance

idavidov13 edited this page Feb 12, 2024 · 2 revisions

Internal Object Properties

Internal Properties

Every object field has four properties:

  • Enumerable - can access all of them using a for…in loop

Enumerable properties are returned using Object.keys method

  • Configurable - can modify the behavior of the property

You can delete only configurable properties

  • Writable - can modify their values and update a property just by assigning a new value to it
  • Value

Inheritance

Types of Inheritance

Single Inheritance (A=>B)

Multilevel Inheritance (A=>B=>C)

Hierarchical Inheritance (A=>B A=>C)

Multiple Inheritance - Not supported in JS with classes, but works with composition - (A=>C B=>C)

The Prototype

What is a Prototype?

Every object in JS has a prototype (template)

  • Internally called proto in browsers and NodeJS
  • Properties lookup follows the prototype chain

Obtained with Object.getPrototypeOf(obj)

Reference to another objects

  • Objects are not separate and disconnected, but linked

Objects inherit properties and methods from a prototype

The prototype property allows you to add new properties to object constructors

Clone this wiki locally