Logo

dev-resources.site

for different kinds of informations.

Prototype and Prototypical Inheritance

Published at
5/10/2022
Categories
javascript
inheritance
es6
Author
tarunsankhla
Categories
3 categories in total
javascript
open
inheritance
open
es6
open
Author
12 person written this
tarunsankhla
open
Prototype and Prototypical Inheritance

Whenever we create an object the Javascript engine automatically attaches your object with hidden properties and functions, the object is accessed via proto within every object that points out (references) the prototype that has been set for that object.

Image description

We can access the properties of the object through the dot operator and can use this property or even update the existing properties

Image description

, and with function when we create a function in JavaScript, the JS engine adds an extra property called prototype to the created function. This prototype property is an object (called a prototype object) that has a constructor property referenced to it by default and inside the prototype property, you will get access to proto object.

Image description

In JavaScript, any function can be attached to an object in the form of a property. An inherited function acts just like any other property and when an inherited function is executed, the value of this points to the inheriting object, not to the prototype object where the function is an own property.

All objects in javascript are references to an object and that object has a prototype of its own and this chain goes on till it is referenced to a null

We can get and set propertytype by Object.getPrototypeOf() and Object.setPrototypeOf(), as well as with non standard proto property. While modifying a prototype you might even come across warnings like

Image description

This is because when navigating through a property object, we basically hop from an object to its [[prototype ]] then its object [[prototype]] until we reach the end of the chain to null. In ES6, there were ways introduced to mutate [[prototype] but they harm the performance in all modern javascript engines. When you set proto, we reduce the future optimizations from Ion on that object, but you also force the engine to go crawling around to all the other pieces of type inference, which involves further deoptimization.

Changing the prototype of an object in the middle of execution is really not a good practice and cause huge performance issue.

inheritance Article's
30 articles in total
Favicon
Code Smell 286 - Overlapping Methods
Favicon
Understanding Traits in PHP and How They Differ from Inheritance
Favicon
Understanding Classes and Inheritance in JavaScript
Favicon
Mastering Generalization in OOP: Techniques and Examples
Favicon
Upcasting — Using a Superclass Reference for a Subclass Object
Favicon
Mapping inheritance hierarchies with MapStruct
Favicon
Python Inheritance Explained: Types, Examples, and Best Practices
Favicon
Mastering TypeScript: Understanding the Power of extends
Favicon
PHP: Herança vs. Composição
Favicon
Method Resolution Order in Python 3
Favicon
Why we don't use RemoteWebDriver driver = new ChromeDriver()
Favicon
Understanding JavaScript Inheritance: A Deep Dive into Prototypal and Constructor Patterns
Favicon
Understanding Inheritance and Polymorphism: Simplified OOP Concepts
Favicon
JS Inheritance - Part 2: Factory Functions vs. Classes
Favicon
Inheritance with access-specifier in cpp
Favicon
Inheritance in Dart
Favicon
RoR - extend, < (inheritance), include - know the difference
Favicon
Inheritance vs composition: a fight against Egyptian gods
Favicon
Object Oriented Concept - Inheritance
Favicon
Exploring Component Inheritance in Aventus
Favicon
Python Inheritance
Favicon
Inheritance vs Composition: Using a Role-Playing Game in JavaScript as an Example
Favicon
PostgreSQL - Partitioning & Inheritance
Favicon
Prototype and Prototypical Inheritance
Favicon
Ruby - Inheritance
Favicon
MultiLevel Inheritance in Java
Favicon
Overloading vs Overriding in Typescript
Favicon
Java Error - at least one public class is required in main file
Favicon
Polymorphism in Java.
Favicon
Method Overriding in Java.

Featured ones: