Skip to content

Instantly share code, notes, and snippets.

@Kishimoto96
Created April 5, 2023 21:57
Show Gist options
  • Save Kishimoto96/68c5b99325ffefd62e6a593a38a020a1 to your computer and use it in GitHub Desktop.
Save Kishimoto96/68c5b99325ffefd62e6a593a38a020a1 to your computer and use it in GitHub Desktop.

Inheritance in JavaScript

  1. What is inheritance in JavaScript, and how does it differ from inheritance in other programming languages?
  2. What is a Prototypal inheritance model?
  3. What is the difference between classical and prototypal inheritance?
  4. What is a constructor function in JavaScript, and how does it differ from a regular function?
  5. How does the .prototype property work in JavaScript, and what is its relationship to constructor functions?
@sncey
Copy link

sncey commented Apr 6, 2023

@saidbaradai @sheidanouri @Mustapha909 @9hello6 @tomiece317

1- Inheritance in JavaScript allows objects to inherit properties and methods from other objects using a prototypal inheritance model. This differs from classical inheritance used in many other programming languages, where objects are created from classes and inherit properties and methods from those classes.
2- Simply put, prototypical inheritance refers to the ability to access object properties from another object. We use a JavaScript prototype to add new properties and methods to an existing object constructor. We can then essentially tell our JS code to inherit properties from a prototype. Prototypical inheritance allows us to reuse the properties or methods from one JavaScript object to another through a reference pointer function.
3- The main difference between classical and prototypal inheritance is the way that objects inherit properties and method. Classical inheritance involves objects being created from classes in a strict class hierarchy, while prototypal inheritance involves objects being created directly from other objects in a prototype chain.Classical inheritance is a more rigid and static inheritance model, while prototypal inheritance is more flexible and dynamic.
4- A constructor function in JavaScript is used to create and initialize objects with specific state and behavior. It differs from a regular function in that it uses the this keyword to refer to the object being created and is typically called with the new keyword. A regular function performs computation or returns a value and can be called with or without new.
5- The .prototype property in JavaScript is used to add properties and methods to constructor functions. When a new object is created using a constructor function with the new keyword, it inherits properties and methods from the constructor function's .prototype object. This allows shared properties and methods to be added to constructor functions and shared among all instances created from that function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment