강의 출처 : The Complete JavaScript Course 2022 Jonas (Udemy)
- Object-oriented programming(OOP) is a programming paradigm based on the concept of objects. (참고 paradigm : Style of code, 'how' we write and organize code)
- We use objects to model(describe) real-world or abstract feature. (real-world e.g. user or todo list item) (abstract features e.g. HTML component or data structure)
- Object may contain data(properties) and code(methods). By using objects, we pack data and the corresponding behavior into one block (data : 변수들 corresponding behavior : methods)
- In OOP, objects are self-contained pieces/blocks of code
- Objects are building blocks of applications, and interact with one another
- Interactions happen through a public interface (API) : methods that the code outside of the object can access and use to communicate with the object
- OOP was developed with the goal of organizing code, to make it more flexible and easier to maintain (avoid 'spaghetti code")
Like a blueprint from which we can create new objects.
ex)
User{
user
password
email
login(password){
// Login logic
}
sendMessage(str){
// Sending logic
}
}
참고) 위의 코드는 단지 개념을 이해하기 위한 예시임 실제 JS내에서 쓰이는 문법과는 조금 다름!
위 class내에서는 user와 관련한 모든 정보가 들어있다.
A real object that we can use in our code, which was created from a class, and a class itself is not an object. Like a real house created from an abstract blueprint.
ex)
new User('jonas')
{
user = 'Jonas',
password = '123sf',
email = dsfasfasf@gmail.com'
login(password){
// Login logic
}
sendMessage(str){
// Sending logic
}
}
Class를 이용하여 우리가 만들고 싶은 만큼 많은 object들을 만들어낼 수 있다.
Abstraction : Ignoring or hiding details that don't matter, allowing us to get an overview perspective of the thing we're implementing, instead of messing with details that don't really matter to our implementaion.
Encapsulation : Keeping properties and methods private inside the class, so they are not accessible from outside the class. Some methods can be exposed as a public interface(API).
Inheritance : Child class extends parent class. Making all properties and methods of a certain class available to a child class, forming a hierarchical relationship between classes. This allows us to reuse common logic and to model real-world relationships.
Polymorphism : A child class can overwrite a method it inherited from a parent class. (It's more complex than that, but enough for our purposes.)
class ---(instantiation)---> Instance
prototype <---(prototypal inheritance / delegation) --- Object
prototype이 가지고있는 methods를 object는 접근할 수 있다.
Objects delegate their behavior(methods) to the prototype, 반면 classical oop 에서는 methods are actually copied from the class to the object.
const num = [1,2,3];
num.map(v => v * 2);
mdn website)
Array.prototype.keys();
Array.prototype.lastIndexOf;
Array.prototype.map();
Array.prototype is the prototype of all array objects. We create in JS. Therefore, all arrays have access to the map method.
즉 Prototypal inheritance 이기에 가능한 것!