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

Team members: @OmarQaqish @noorin99 @MOHAMMAD-ALMOHAMMAD @zeynepdumlupinar, Talal Bakkour

  1. Inheritance in javascript is used to inherit attributes and methods of a class. In JavaScript, objects are created directly from other objects, which are seen as prototypes, and every object has a prototype chain. This is different from classical inheritance in other languages, which involves creating objects from classes that define their properties and methods.
  2. The linking of prototypes of a parent object to a child object to share and utilize the properties of a parent class using a child class. It is a model of inheritance in which an object can inherit properties and methods directly from another object, known as its prototype.
  3. The main difference between classical and prototypal inheritance in JavaScript is that in classical inheritance, objects are created from classes, while in prototypal inheritance, objects are created directly from other objects (prototypes). Additionally, classical inheritance follows a strict hierarchy, whereas prototypal inheritance allows for more flexibility in object creation and inheritance. In the prototype model, each new instance of an object will have a parent due to the prototype chain, in the classical model, this isn't always the case
  4. Constructor functions creates a new instance of the context its in, regular functions don't and they would point to the same reference in memory and they are of type object.
  5. In JavaScript, every function has a property called .prototype, which is an object that serves as the prototype for any objects created with that function as a constructor. When a constructor function is called with the new keyword, a new object is created and linked to the constructor's .prototype object, which allows the new object to inherit properties and methods from it.

@cansucreates
Copy link

Team members: Tasneem Akkad, Atakan Serbes, Rayan Alrouh, Cansu Aysagdic.

  1. A class can inherit all the methods and properties of another class. When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype.
  2. Prototypes are hidden objects that are used to share the properties and methods of a parent class to child classes.
  3. Classical Inheritance is a mechanism in which one class can extend the methods of another class. Key Ideas: Classes may inherit from other classes. Objects are created from classes. Classes are immutable they can not be modified at runtime. But objects can be modified at runtime. The main difference between classical and prototypal inheritance is the way objects inherit properties and methods from their parent objects.
  4. Constructor function in js is a function that is used with a new keyword to create objects. the purpose of constructor to define properties and methods for an object that is created. The main difference is constructor only works with new key. and does not have any return. but regular function does have a return.

@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