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?
@motaz99
Copy link

motaz99 commented Apr 6, 2023

Motaz Ali, Joud Khanji, Abdulsalam Hamandoush, Ismail Dincer, Sara nafisa:

  1. inheritance in JS is when you create a subclass from a parent class, and this subclass or child class will inherit the methods and properties from the parent class, the difference between JS inheritance is that JS use prototype-based inheritance, but the others like Java or C++ use a real OOP classes

@omikay
Copy link

omikay commented Apr 6, 2023

Omid Kayhani | Khaled Naes | Yasir Irzooqi | Hande Nur Demirbay

  1. In JavaScript, inheritance is implemented using prototypes, which are objects that serve as templates for other objects. It differs from inheritance in other programming languages in that JavaScript uses prototypal inheritance rather than class-based inheritance.
  2. Prototypal inheritance is a way of implementing inheritance in JavaScript, where objects can inherit properties and methods from other objects, which are used as prototypes.
  3. Classical inheritance is static and doesn't allow for dynamic changes to an object's inheritance hierarchy as prototypal does.
  4. A constructor function in JavaScript is a special function that is used to create objects, and it differs from a regular function in that it is typically called with the new keyword and sets the this keyword to refer to the new object it creates.

@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