Sprint 7 Flashcards
Introduction to Object-Oriented Programming (35 cards)
Encapsulation
Encapsulation in JavaScript is a mechanism of bundling data (attributes) and methods that operate on that data within a single unit (object).
It restricts direct access to some of the object’s components, preventing unintended modification and promoting data integrity.
This is achieved by controlling access to the internal state of an object, often using closures or conventions to define private and public members. Encapsulation helps in organizing code, managing complexity, and implementing data hiding.
TripleTen OOP Lesson and definition of Encapsulation
https://tripleten.com/trainer/web/lesson/e968713b-9ec3-4f49-b701-2898a2c1c1ac/task/4f0f0797-f0fd-416b-b78c-04c46bf75f36/?theory_open=false
*ex.
The inner workings of a program are often hidden from the outside world. We call this encapsulation. The main advantage of this is that it hides away the complexity of the device, leaving the user with a convenient and intuitive way of interacting with it.
Inheritance
In object-oriented programming, inheritance means creating a class based on another class.
- In JavaScript, inheritance allows programmers to reuse previously written classes. We can have child classes or parent classes, also referred to as subclasses and superclasses respectively. This helps them remove excessive code duplication and makes code much easier to maintain.
Polymorphism
*TripleTen Definition
You’ve just seen the concept of polymorphism in action, which is the ability of objects with the same interface to have a different implementation.
Link to lesson for deeper understanding
https://tripleten.com/trainer/web/lesson/03ac1894-88e2-4436-a2f5-b31bf1de8661/task/6b15a92b-02b5-4fa2-80ca-f05428073879/
*Google definition
Polymorphism, meaning “many forms,” is a core concept in object-oriented programming (OOP) that allows objects of different types to be treated as objects of a common type. In JavaScript, polymorphism is primarily achieved through prototype inheritance and method overriding. It enables writing more flexible and maintainable code by allowing different objects to respond to the same method call in their own specific ways.
ex.
class Animal {
constructor(name) {
this.name = name;
}
speak() {
return “Generic animal sound”;
}
}
class Dog extends Animal {
speak() {
return “Woof!”;
}
}
class Cat extends Animal {
speak() {
return “Meow!”;
}
}
const animal = new Animal(“Generic Animal”);
const dog = new Dog(“Buddy”);
const cat = new Cat(“Whiskers”);
console.log(animal.speak()); // Output: Generic animal sound
console.log(dog.speak()); // Output: Woof!
console.log(cat.speak()); // Output: Meow!
const animals = [dog, cat];
animals.forEach(animal => {
console.log(animal.speak()); // Output: Woof!, Meow!
});
Note
In this example, the speak method is polymorphic. Even though we call the same method on different objects (dog and cat), they produce different outputs. This is because the Dog and Cat classes have overridden the speak method of their superclass Animal. The animals array demonstrates how different objects can be treated as a common type (Animal) and still exhibit their specific behaviors.
What is a Program Paradigm?
A programming paradigm is an approach to coding based on a coherent set of shared principles across a particular discipline.
According to OOP, code must comply with what requirements?
- It is represented by a set of interacting objects that make up a single structure called a program. Objects store data and have methods that enable that data to be passed from one object to another.
- It relies on the key OOP concepts, which are encapsulation, inheritance, and polymorphism.
What is a method?
A method, generally speaking, is any function that’s performed on an object.
We should also take a moment to note that any function in JavaScript is a method, even if it’s declared globally.
What is an alert() method?
What is an split() method?
What is an indexOf() method?
What is an object method?
The behavior of an object is defined via functions, called object methods. In the code below, song.like is a method of the song object.
ex.
const song = {
title: “Circles”,
artist: “Mac Miller”,
isLiked: false,
like: function () {
song.isLiked = !song.isLiked;
}
};
What is an object “state”?
The object state includes its variables and properties, also called fields.
What is memory consumption?
What is “this”?
this - is a JavaScript keyword that refers to an object. The value of that object can be dependent on the context where we find “this.”
What is a string interpolation or template literal?
${}
It’s a way to embed expressions directly within a string. Instead of concatenating strings with the + operator, you can use backticks (`) to define the string and place variables or expressions inside ${}.
ex.
const name = “Alice”;
const age = 30;
const message = My name is ${name} and I am ${age} years old.
;
console.log(message); // Output: My name is Alice and I am 30 years old.
“class” Naming Convention
We always write a class name in TitleCase
ex. TitleCase
“new” keyword
The new keyword need to be included whenever we create a new class instance
constructor() function
Each class comes equipped with a constructor() function, which takes arguments in the (…, …).
Each time an object is created, a constructor function would automatically run it’s code and return a newly created object.
ex.
class Song {
constructor(title, artist) {
// run some code
this.artist = artist;
// return new song
this.title = title;
}
}
What is the Prototype Chain?
The “class” keyword
A class defines the data and functionality that an object will possess.
Think of it as a blueprint used to create objects. Such objects are called class instances.
Classes declare properties (data) and methods (functionality) in their objects.
What is ES6?
ES6 is the sixth version of the ECMAScript specification introduced in 2015.
That’s why it’s sometimes called “ES2015”.
What is a User Interface?
In programming, we can often come across the term “user interface.” When we talk about a user interface, we’re referring to the appearance of the program. This includes things like the user menu, any buttons or switches, input fields, and so on. This is what allows our users to interact with a program, and acts as the medium between the program’s code and the user.
Any complex device must have an easy-to-use interface. It’s much simpler to start a car using a key instead of popping open the hood and trying to manually start the engine yourself.
Emulate
Emulation is the ability of one computer program to imitate the functionality of another program.
Interface Principle in OOP
In OOP, the same principle is applied to objects. Outer objects of the interface are the public methods you can safely call in external code.
In addition to public methods, objects also have private methods and properties. The difference between these is that private methods are solely intended for the internal workings of an object.
What does Inheritance mean in OOP?
In object-oriented programming, inheritance means creating a class based on another class.
- In JavaScript, inheritance allows programmers to reuse previously written classes. We can have child classes or parent classes, also referred to as subclasses and superclasses respectively. This helps them remove excessive code duplication and makes code much easier to maintain.