Skip to content

Instantly share code, notes, and snippets.

@Kishimoto96
Created March 31, 2023 22:52
Show Gist options
  • Save Kishimoto96/2a4a8c96630887e4a8f9940db41240e4 to your computer and use it in GitHub Desktop.
Save Kishimoto96/2a4a8c96630887e4a8f9940db41240e4 to your computer and use it in GitHub Desktop.

object oriented programming (OOP)

  1. What are some advantages of using object-oriented programming (OOP) in JavaScript, and how does it differ from other programming paradigms?
  2. What are some common design patterns used in object-oriented programming, and how can they help to solve recurring software development problems?
  3. Can you explain the difference between classical inheritance and prototypal inheritance in JavaScript, and provide some examples of each?
  4. How do you define a class in JavaScript, and what are some best practices for creating and using classes effectively?
  5. What is the role of constructors and prototypes in OOP in JavaScript, and how do they relate to each other?
@houzifahabbo
Copy link

Team members: @houzifahabbo ,@nour Eddin Hamouda ,@MOHAMMAD-ALMOHAMMAD ,@atakan Serbes
1- It gives us the ability to inherit classes and encapsulates them, it prevents redundancy in code and allows you to not repeat the code you write, and it is more secure.
2- there are a lot of patterns like :

  • singleton pattern: this pattern includes only making one instance of a class and providing global access to this instance
  • Structural: Adapter — interface to an object. Bridge — implementation of an object. Composite — structure and composition of an object. Facade — interface to a subsystem. Flyweight — storage costs of objects. Proxy — how an object is accessed. Decorator — responsibilities of an object without subclassing.
  • A factory is an object or class or a function in a functional programming paradigm for creating objects.

3- In classical inheritance models inheritance occurs when an object instance inherits from a class and a subclass can inherit from a parent class, Prototypal inheritance, on the other hand, supports an object inheriting from any other object rather than from classes.

const animal = {
  speak() {
    console.log(`${this.name} makes a sound.`);
  }
}

const dog = Object.create(animal);
dog.name = 'Rufus';

const cat = Object.create(animal);
cat.name = 'Whiskers';

dog.speak(); // -> "Rufus makes a sound."
cat.speak(); // -> "Whiskers makes a sound."

4- we can make a new class by either using the "new" keyword or setting its prototype to the class prototype, as for best practices, its better to inherit from a parent class instead of repeating the code
such as if we have an Animal class, instead of making classes for all types of animals we can have those classes inherit from the parent animal class.

class Animal {
  constructor(name) {
    this.name = name;
  }

  speak() {
    console.log(this.name + ' makes a noise.');
  }
} 
class Lion extends Animal {
  constructor(name) {
    super(name)
  }

  speak() {
    console.log(this.name + ' Roars.');
  }
}

5- Constructors and prototypes work together to implement inheritance and method sharing in JavaScript. A constructor is used to create and initialize objects, while a prototype is used to share properties and methods among all instances of a constructor function.

class Box{
Box(value) {
  this.value = value;
}
}

// Properties all boxes created from the Box() constructor
// will have
Box.prototype.getValue = function () {
  return this.value;
};

const boxes = [new Box(1), new Box(2), new Box(3)];

@fatimaali200
Copy link

team: fatima , harith riyadh, ahmed rashed, hande nur demirbay

1-allows for code reusability, as code can be written in a modular manner and can be reused in other parts of the application.
2-Singleton Pattern , Factory Pattern, Observer Pattern, Adapter Pattern,Decorator Pattern
3-Classical Inheritance:a subclass can inherit properties and methods from a superclass.
protptypal : objects are created first, and then other objects are created by cloning or inheriting from them.
4- a class is defined using the class keyword.allows for code reusability, as code can be written in a modular manner and can be reused in other parts of the application.
5-constructors and prototypes work together to create and define the properties and behaviors of objects in JavaScript. Constructors are used to create new objects, while prototypes are used to define their properties and methods.

@TasneemAkkad
Copy link

  1. there are a lot of them some of them are singleton, Factory, Observer, Decorator, and Adapter patterns, we use them to solve recurring software development problems. some of them provide a proven solution to common problems and make code more flexible, maintainable, and extensible also these patterns can help developers save time and effort, improve code quality, and create more robust and scalable software applications.
  2. In classical inheritance, classes are defined using a blueprint or template, and objects are created from these classes using the new keyword, n prototypal inheritance, objects inherit properties and methods directly from other objects. There are no classes, and objects are created using constructor functions or object literals.
  3. In JS, classes can be defined using the class keyword, it can help you organize and structure your code in a more object-oriented way
  4. The prototype is the base class, the constructor your current class. In OOP the above would be class Foo extends Function You can also start inheritance with this entire setup of prototype and constructor making more complex objects as you go whilst sharing functionality.

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