toggle-toggle / javascript-basic

🌱우아한 테크코스 프론트엔드 자바스크립트 기초 스터디 입니다.
9 stars 0 forks source link

[2021.03.15] 클래스, 팩토리 #6

Closed shinsehantan closed 3 years ago

shinsehantan commented 3 years ago

클래스

1. 클래스란?

2. 클래스의 구조

Class body

constructor(생성자)

3. class 표현식과 class 선언

class 선언

class ToggleStudy {
    constructor(boss, member) {
        this.boss = boss;
        this.member = member;
    }
}

class 표현식

// 클래스 이름이 없는 표현식

let ToggleStudy = class {
    constructor(boss, member) {
        this.boss = boss;
        this.member = member;
    }
}
console.log(ToggleStudy.name) // ToggleStudy
// 클래스 이름이 있는 표현식

let ToggleStudy = class ToggleStudy2 {
constructor(boss, member) {
    this.boss = boss;
    this.member = member;
    }

    sayHi() {
        console.log(ToggleStudy2)
    }
}
console.log(ToggleStudy.name) // ToggleStudy2
스크린샷 2021-03-14 오후 7 55 57 스크린샷 2021-03-14 오후 7 54 16

4. 클래스 동작 원리

class Study {
    // 생성자
    constructor(name) {
        this.name = name;
    } 

    sayName() {
        console.log(this.name)
    }
}

// 인스턴스 생성
const toggleStudy = new Study('toggle')

toggleStudy.sayName(); // toggle
console.log(toggleStudy instanceof Study) // true

5. 정적 메서드(static)

Study.sayHi(); // hi

const study = new Study() study.sayHi() // Uncaught TypeError: study.sayHi is not a function

- 정적 메서드는 this를 사용할 수 없고, 정적 메서드 내부에서의 this는 클래스 인스턴스가 아닌 클래스 자신을 가리킴.
- 정적 메서드에서 this를 사용할 수 없다는 뜻은 메서드 내부에서 this를 사용할 필요가 없는 메서드는 정적 메서드로 만들 수 있다는 의미
- 애플리케이션 전역에서 사용할 유틸 함수를 생성할 때 자주 사용됨.
- 유틸리티 클래스는 상태를 관리하지 않는 클래스라고 볼 수 있음.
- Math 객체의 메서드도 static 메서드의 형식`Math.max() Math.min()`
- 어떤 메서드를 사용해야 할 때, 해당 메서드가 인스턴스의 변수를 사용하지 않고, 인스턴스가 생성되지 않았더라도 사용해야 한다면 정적 메서드를 사용하면 됨.

### 6. Getter와 Setter
#### getter(획득자)
- 클래스 필드에 접근할 때마다 클래스 필드의 값을 조작하는 행위가 필요한 경우 사용
- 메서드 이름 앞에 get 키워드를 붙여서 정의
- getter는 반드시 return 반환값이 필요

let user = { name: "John", surname: "Smith",

get fullName() { return ${this.name} ${this.surname}; } };

alert(user.fullName); // John Smith

- 일반 프로퍼티에서 값에 접근하는 것처럼 평범하게 user.fullName을 사용해 프로퍼티 값을 얻을 수 있음.
- 위의 코드에는 setter가 없기 때문에 user.fullName = "something"으로 값을 할당하려고 하면 에러가 발생함.

#### setter(설정자)
- 클래스 필드에 값을 할당할 때마다 클래스 필드의 값을 조작하는 행위가 필요한 경우 사용
- 메서드 이름 앞에 set 키워드를 붙여서 정의

let user = { name: "John", surname: "Smith",

get fullName() { return ${this.name} ${this.surname}; },

set fullName(value) { [this.name, this.surname] = value.split(" "); } };

// 주어진 값을 사용해 set fullName이 실행됨. user.fullName = "Alice Cooper";

alert(user.name); // Alice alert(user.surname); // Cooper


-----

# 팩토리

### 1. 팩토리란?
- 객체를 반환하는 함수
- 자바스크립트에서는 모든 함수가 객체를 리턴할 수 있는데, 그중에서도 new 키워드가 없는 경우를 팩토리 함수라고 볼 수 있음. 
- 클래스와 new 키워드의 복잡함 없이 객체 인스턴스를 쉽게 생성 할 수 있음. (필요하지는 않지만, new 키워드를 사용해도 무방)
- 팩토리 함수명에는 create 접두사를 붙여서 명확한 의도를 전달할 수 있다. `createJelly(){}`

function createDessert() { return { type: 'dessert', bowl: [ createJelly(), createIceCream() ] }; }


- jelly와 iceCream 팩토리로 생성된 객체를 반환하는 dessert 팩토리 함수를 만들 수 있음.

# 클래스 vs 팩토리
- 둘 다 객체를 생성하는 함수
- 캡슐화(내부 변수, 감추고 싶은 함수에 접근 가능한가)
  - 클래스 : 캡슐화 불가능
  - 팩토리 : 캡슐화 가능
- 불변성(한 번 정의된 함수가 변경되는가)
  - 클래스 : 변경 가능
  - 팩토리 : 변경 불가
- 메모리 소비
  - 클래스 : 메서드는 프로토타입 객체에서 한 번만 생성되어 모든 인스턴스에서 공유되기 때문에 팩토리에 비해 우수함.
  - 팩토리 : 수천 개의 동일한 객체를 만들 경우 클래스보다 메모리 비용이 많이 듦.

---

### 출처
- 클래스
https://corock.tistory.com/472
https://ko.javascript.info/class
https://ko.javascript.info/static-properties-methods
https://mygumi.tistory.com/253
https://ko.javascript.info/property-accessors

- 팩토리
https://ui.toast.com/weekly-pick/ko_20160905
https://tpgns.github.io/2018/04/08/javaScript-factory-function-with-es6/#fnref1
https://velog.io/@godori/factory-method-pattern

- 클래스 vs 팩토리
https://simsimjae.tistory.com/373
https://steemit.com/kr/@wonsama/javascript-class-vs-factory

- 스트릭트 모드
https://beomy.tistory.com/13