- What are some advantages of using object-oriented programming (OOP) in JavaScript, and how does it differ from other programming paradigms?
- What are some common design patterns used in object-oriented programming, and how can they help to solve recurring software development problems?
- Can you explain the difference between classical inheritance and prototypal inheritance in JavaScript, and provide some examples of each?
- How do you define a class in JavaScript, and what are some best practices for creating and using classes effectively?
- What is the role of constructors and prototypes in OOP in JavaScript, and how do they relate to each other?
-
-
Save Kishimoto96/2a4a8c96630887e4a8f9940db41240e4 to your computer and use it in GitHub Desktop.
- Abdulrahman Albakkar, 2. Iroda Yilmaz, 3. Nur Abunamus, 4. Rasam Rabiee
Q1. OOP allows us to reuse code so it reduces redundancy
It allows using polymorphism, encapsulation, inheritance and abstraction.
Q2. Factory, Singleton, Decorator, Adaptor, Bridge, Strategy Observer, Facade. Those patterns support creation of objects
Q3. Classical inheritance is based on the idea of classes and objects.
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(${this.name} makes a noise.
);
}
}
prototypal inheritance is based on the idea of prototypes.
class Dog extends Animal {
speak() {
console.log(${this.name} barks.
);
}
}
Q4. Use classes to model real-world objects or concepts. Classes should represent a logical grouping of data and behavior.
Use meaningful names for classes and methods. Choose names that are descriptive and easy to understand.
Keep classes small and focused. Each class should have a single responsibility and should be easy to understand and maintain.
Use the constructor method to initialize class properties. This method is called when a new object is created from the class.
Use inheritance and composition to reuse code and promote modularity.
Use access modifiers like public, private, and protected to control access to class properties and methods.
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHello() {
console.log(Hello, my name is ${this.name} and I am ${this.age} years old.
);
}
}
Q5. constructors are used to create and initialize objects, while prototypes are used to share methods and properties among multiple objects. They work together to enable object-oriented programming in JavaScript.
Room 7: Mohamad Aid Bacuk Zanab, Muhammed Bekkur, Abdullah Amin, Younes Nourzehi
1- for the reusability of code, having related functions and variables together, to hide the complexity of an object and to provide a simple interface, allows you to inherit properties from a parent object, overwriting functions inside an object
2-Creational: These patterns are designed for class instantiation. They can be either class-creation patterns or object-creational patterns.
Structural: These patterns are designed with regard to a class's structure and composition. The main goal of most of these patterns is to increase the functionality of the class(es) involved, without changing much of its composition.
Behavioral: These patterns are designed depending on how one class communicates with others.
3- classical inheritance uses classes and a hierarchy of inheritance to create objects, while prototypal inheritance uses objects and a prototype chain to create objects.
4- we use the "class" keyword (ES6) or factory functions (in older versions of JS). OOP has 4 pillars: Encapsulation, Abstraction, Inheritance, and Polymorphism. also we should use constructors, getters, setters, and name conventions.
5- constructors are used when we are creating objects (with the new keyword) and setting their default properties. but the prototypes define the methods of an object that can be inherited
Team members: M. Nour Krimesh, Sara Nafisa, Ahmad Ramin Soleyman Kheyl, Omar Qaqish
- Reusability: OOP allows you to create reusable code. You can create classes, which define the structure and behavior of objects, and then create instances of those classes. This means you can reuse code without having to rewrite it.
Encapsulation: OOP allows you to encapsulate data and behavior within an object. This means that the data and behavior are protected from outside interference and can only be accessed through the object's methods.
Abstraction: OOP allows you to abstract away the details of an object's implementation. This means that you can use objects without having to know how they work internally.
Inheritance: OOP allows you to create new classes that inherit properties and methods from existing classes. This means you can reuse code and create new classes that have similar behavior to existing ones.
OOP organizes programs as objects: data structures consisting of attributes and methods together with their interactions, while procedural programming specifies the steps of a program, and functional programming uses functions.
-
Creational (singleton design pattern), structural (decorator design pattern), and behavioral (command design pattern). These patterns, and many others, help to solve common problems in software development by providing a tried-and-true solution that has been used successfully in other projects. They can help to reduce development time, increase code quality, and make code easier to maintain and refactor over time. By using these patterns, developers can focus on the unique aspects of their project, rather than reinventing the wheel for each new problem they encounter.
-
Classical inheritance is limited to classes inheriting from other classes while prototypal inheritance supports the cloning of any object using an object linking mechanism.
Prototypal inheritance:
let animal = { name: 'Animal', speak() { console.log(
${this.name} makes a noise.`);
}
};
let dog = Object.create(animal);
dog.name = 'Rex';
dog.speak(); // output: "Rex makes a noise."`
Classical inheritance:
`class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(${this.name} makes a noise.
);
}
}
class Dog extends Animal {
speak() {
console.log(${this.name} barks.
);
}
}
let dog = new Dog('Rex');
dog.speak(); // output: "Rex barks."`
- To define a class in JavaScript, you can use the class keyword followed by the class name, curly braces containing the class methods and properties, and a constructor method.
Best practices:
- Call things by their name — easy, short and readable variable and function names
- Avoid globals
- Stick to a strict coding style
- Comment as much as needed but not more
- In object-oriented programming (OOP) in JavaScript, constructors are used to create and initialize objects. They are functions that are called with the new keyword and return a new instance of an object. Constructors are used to create and initialize objects, while prototypes are used to add methods and properties to objects created by a constructor. They work together to create object-oriented code in JavaScript.
Student names : @sack-ali @radmanlo @talal-bakkour khaled naes
1-The main difference between OOP and other programming paradigms is that OOP is let us use resue our code again and its centered around objects, while other paradigms are centered around functions or data. OOP also promotes code reusability, encapsulation, and abstraction, which are not always emphasized in other paradigms.
-Modularity for easier troubleshooting.
-Reuse of code through inheritance.
-Flexibility through polymorphism.
-Effective problem solving.
2- Some design patterns are Singleton Pattern, Factory Pattern, Observer Pattern, Strategy Pattern, Decorator Pattern, So, design patterns are reusable solutions to common software design problems that can help developers write more efficient and maintainable code.
3-classical inheritance is based on classes and a hierarchical class structure, while prototypal inheritance is based on prototypes and copying properties and methods from one object to another. Both approaches can be used effectively in JavaScript, depending on the specific requirements of the application.
4- class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(Hello, my name is ${this.name} and I'm ${this.age} years old.
);
}
}
const john = new Person('John', 30);
john.greet(); // Output: Hello, my name is John and I'm 30 years old.
- constructors are used to create new objects and initialize their properties, while prototypes are used to share properties and methods among all instances of the same object. They work together to create a powerful and flexible system for object-oriented programming in JavaScript.
Motaz Ali, Tareq Harh Muhammmrd Hasan, Guled Khadar Abd, Ammmar Almuain.
1-advantages of using object-oriented programming: - Encapsulation: Reduce complexity + Increase reusability- Abstraction: Reduce complexity + isolate impact of changes- Inheritance: Eliminate redundant code- Polymorphism: Refactor ugly switch/case statements , This differs from other programming paradigms such as procedural programming, where code is organized into procedures or routines that perform specific tasks.
2- encapsulation; abstraction; inheritance; polymorphism;
3-Classical inheritance is limited to classes inheriting from other classes. However prototypal inheritance includes not only prototypes inheriting from other prototypes but also objects inheriting from prototypes.
4-One of the best practices of classes is the inhertance feature, so we can define a class for example Animal and we put inside this Animal class the common functionallity for example breathing, so all alnimals can breath, so all elements should inherate this method so we don't need to write it over and over again, and also we can add additional functionality for the subClasses, so for example not all animals have lege and so on..s
5- constructors are used to create and initialize objects, while prototypes are used to define shared properties and methods that are inherited by all objects created using the constructor.
Team members: @aydinfz , @jimaamaya, @hishamalwatter, @cansucreates.
- Reusability, Encapsulation hides the implementation details of an object and expose only the necessary information through its public interface, Abstraction hiding the details of the implementations. By inheritance, reduce code duplication and make your code more modular. OOP allows you to create objects that can take on different forms depending on their context. This can make your code more flexible and adaptable.
- Singleton Pattern, Factory Method Pattern, Abstract Factory Pattern, Builder Pattern, Prototype Pattern. Design patterns are reusable solution to commonly occurring problem within a given context in Software design. It's not a finished design that can be transformed directly into app. Rather, it is a template to solve a recurring design problem in code.
- Classical inheritance is a type of inheritance that is based on the concept of classes. In classical inheritance, objects inherit properties and methods from classes. Classes are like blueprints or templates that define the structure and behavior of objects. In contrast, prototypal inheritance is based on the concept of prototypes. In prototypal inheritance, objects inherit properties and methods from other objects. Objects in JavaScript are essentially just collections of key-value pairs, and each object has an internal prototype property that points to another object.
This is classical example:
class Animal {
constructor(name, age) {
this.name = name;
this.age = age;
}
speak() {
console.log(`My name is ${this.name}, and I'm ${this.age} years old.`);
}
}
class Dog extends Animal {
constructor(name, age, breed) {
super(name, age);
this.breed = breed;
}
bark() {
console.log('Woof!');
}
}
const myDog = new Dog('Max', 3, 'Labrador');
myDog.speak(); // My name is Max, and I'm 3 years old.
myDog.bark(); // Woof!`
prototypical example:
// Define a parent object
var Animal = {
name: '',
setName: function(name) {
this.name = name;
},
speak: function() {
console.log(`${this.name} makes a noise.`);
}
};
// Define a child object that inherits from the parent object
var Cat = Object.create(Animal);
Cat.speak = function() {
console.log(`${this.name} meows.`);
};
// Create a new Cat object
var myCat = Object.create(Cat);
// Set the cat's name
myCat.setName('Whiskers');
// Call the cat's speak method
myCat.speak(); // Output: "Whiskers meows."
- You can define a class using the class keyword:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
- A constructor is a function that is used to create objects of a particular class. When a new object is created using the new keyword, the constructor function is called, and it sets up the initial state of the object. Constructors typically define instance properties and methods that are specific to the object being created. Prototypes, on the other hand, are objects that serve as a blueprint or template for other objects. Every JavaScript object has a prototype, which is an object that it inherits properties and methods from. When you try to access a property or method on an object and that property or method is not defined on the object itself, JavaScript looks up the prototype chain to find the property or method on the object's prototype or its prototype's prototype, and so on. Constructor is a special method that is called when an object is created using the new keyword. It is used to initialize the object's properties and set up its state. In JavaScript, constructors are defined using a function, which is invoked with the new keyword to create an instance of an object.
So in summary, constructors are used to create and initialize objects, while prototypes define the shared properties and methods that are inherited by all instances of an object. The two work together to enable inheritance and polymorphism in JavaScript OOP.
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.sayHello = function() {
console.log("Hello, my name is " + this.name + " and I am " + this.age + " years old.");
};
Members: İsmail Dincer, Aslı Sema Gültekin, Rawan Kamal Mustafa, Muhammed Mustafa Alnaddaf.
1- OOP makes it easier to debug and trouble-shoot. We can reuse the code via classes. It makes the code more organized and compound. In JavaScript it uses prototypes, others don't.
2- We can use classes to prevent inventing new functions for every other instance. We can also use constructor functions and Object.create ().
3- ın classıcal ınherıtance we ınherıt the class varıables and functıons, Prototype inheritance in javascript is 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. Prototypes are hidden objects that are used to share the properties and methods of a parent class to child classes.
4- class User {
name = "John";
sayHi() {
alert(Hello, ${this.name}!
);
}
}
new User().sayHi(); // Hello, John!
5- Constructor is initilized and executed automatically when a new object is created. It is used to initilized object properties.
Adib Naser, Özlem keleş, Mahmoud Alshaheen, Mustafa Noori
1- difffer : divided into object and method,
advantages : put the data in one location, Code reusability: encapsulation is hiding the implementation details of an object from the outside world. This enables developers to create reusable code and helps to reduce the amount of duplicated code, Modularity for easier troubleshooting
Reuse of code through inheritance
Flexibility through polymorphism
Effective problem solving
2- Design patterns is the most effective way to handle practical problem , the help by Using these patterns is considered good practice, as the design of the solution is quite tried and tested, resulting in higher readability of the final code. Design patterns are quite often created for and used by OOP Languages, like Java, in which most of the examples from here on will be written. example : Singleton Design Pattern
Factory Design Pattern .
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. Array is inhrentce prototype js
4- A class can be defined in two ways: a class expression or a class declaration.
5- Constructor Functions
When we create a constructor function, the convention is to start its name with a capital letter.
Arrow function won't work as a constructor function, because it doesn't have its own this keyword.
This constructor function can be used to make any number of objects as we want.
Prototypes Functions:
on the other hand, are objects that are used to share properties and methods between multiple instances of an object. Every object in JavaScript has a prototype object, which is used to inherit properties and methods from.
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)];
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.
- 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.
- 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.
- 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
- 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.
group members: Abdullah karkarli, abdulsalam hamandoush, barrah masri, nezir aydin
for example:
classical inheritance:
Person john = new Person();
john.setName("John");
john.setAge(30);
prototypal inheritance:
var person = {
name: "",
age: 0,
getAge: function() {
return this.age;
},
getName: function() {
return this.name;
}
};
var john = Object.create(person);
john.name = "John";
john.age = 30;
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
getAge() {
return this.age;
}
getName() {
return this.name;
}
}
const ahmed = new Person('ahmet', 30);
Use the constructor method to set up object properties. This is where you can initialize any properties that the object will need.
Define methods on the class prototype. This saves memory because the method is shared among all instances of the class, rather than being created anew for each instance.
Use static methods sparingly. Static methods are methods that are called on the class itself, rather than on instances of the class. They can be useful in certain situations, but they can also make code harder to test and maintain.
Use inheritance judiciously. Inheritance can be a powerful tool, but it can also lead to complex and hard-to-maintain code. Use it only when it makes sense, and consider using composition (combining smaller, simpler objects) instead.
Use classes to encapsulate related behavior. A class should represent a cohesive concept or object in your application, with methods that are related to that concept or object.
Use the extends keyword for subclassing. If you need to create a subclass that inherits from another class, use the extends keyword to do so. This helps to make your code more readable and easier to maintain.
For example, consider the following constructor function:
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.sayHello = function() {
console.log(
Hello, my name is ${this.name}
);};