자바스크립트 - OOP(객체지향프로그래밍)

코드위의승부사·2019년 12월 14일
1

JavaScript

목록 보기
5/8

절차지향프로그래밍과의 비교

절차지향프로그래밍(Procedural Programming)은 함수들로 이루어진 프로그래밍이다.
데이터는 변수들에 저장되있고 함수들이 그 데이터를 처리한다.
이 함수들이 상호의존적이기 때문에 문제를 일으키기 쉽다.

반면에 객체지향프로그래밍(Object Oriented Programming)은 관련있는 변수와 함수를 하나의 단위(객체)로 결합시킨다.
여기서 변수를 Property 그리고 함수를 Method라고 한다. (ex. localStorage)

객체지향프로그래밍의 특징

Encapsulation(캡슐화)

관련있는 변수와 함수를 하나로 묶고 외부에 공개될 필요가 없는 정보는 숨기는 것이다.(은닉화)
Hide Implementation(Modify anytime) and Expose behavior

let baseSalary = 30_000;
let overtime = 10;
let rate = 20;

// procedual code
function getWage(baseSalary, overtime, rate) {
	return baseSalary + (overTime * rate);
}

// object-oriented code
let employee = {
  baseSalary: 30_000,
  overtime: 10,
  rate: 20,
  getWage: function() {
    return this.baseSalary + (this.overtime * this.rate);
  }
};
employee.getWage();

"The best functions are those with no parameters!" - Robert C Martin

Abstraction(추상화)

몇가지 메소드나 프로퍼티를 숨겨놓는것
Simpler Interface
Reduce the Impact of Change

Inheritance(상속)

같은 메소드와 프로퍼티를 공유하면서 사용할 수가 있다.
eliminate reduant code

Polymorphism(다형성)

HTMLElement - render method
렌더 메소드를 참조하는 객체의 타입에 따라 다르게 사용할 수 있다.

Benefit of OOP

Encapsulation - Reduce complexity + increase resuability
Abstraction - Reduce complexity + isolate impact of changes
Inheritance - Eliminate redundate code
Polymorphism - Refactor ugly switch/case statements

Reference

profile
함께 성장하는 개발자가 되고 싶습니다.

0개의 댓글