Coder Social home page Coder Social logo

oop's Introduction

Object-Study

Contents

πŸ“š μ±… μŠ€ν„°λ”” 진행 κ°œμš”

였브젝트 : μ½”λ“œλ‘œ μ΄ν•΄ν•˜λŠ” 객체지ν–₯ 섀계 책을 μ½μœΌλ©΄μ„œ μ •λ¦¬ν•œ λ‚΄μš©μž…λ‹ˆλ‹€. 였브젝트 μŠ€ν„°λ”” 레포 링크


예제 μ½”λ“œ

각 μ±•ν„°λ³„λ‘œ μ˜ˆμ œμ½”λ“œλŠ” Typescript + Factory Function 으둜 μž‘μ„±ν•΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€. Factory Function 의 νŠΉμ§•κ³Ό, μž₯점은 μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.

Factory Function μ΄λž€?

1. Factory Function κΈ°λ³Έ μ‚¬μš© 예제
function TodoModel(data){
  const todos = [];

  function addData() {
    console.log(`${data} addData`);
  }

  function add() { console.log('add'); }

  return Object.freeze({
    addData,
  });
}

const todoModel = TodoModel('input');
todoModel.addData();        // input addData
2. μΊ‘μŠν™” (Encapsulation)

factory function 을 μ‚¬μš©ν•΄ javascript class 의 private class fieldsλ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ³  μΊ‘μŠν™” (Encapsulation)λ₯Ό κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

const todoModel = TodoModel('input');
console.log(todoModel.todos);       // undefined
console.log(todoModel.data)         // undefined
todoModel.add();                    // todoModel.add is not a function
3. λΆˆλ³€μ„±(Immutable)

μ •μ˜λœ ν•¨μˆ˜λ₯Ό λ³€κ²½ν•  수 μ—†μŠ΅λ‹ˆλ‹€.

todoModel.add = function() {
  console.log('a new add');
}
todoModel.add();            // add
4. 상속과 ν•©μ„±(Inheritance and Composition)

factory function μ—μ„œλŠ” 합성을 톡해 μ½”λ“œλ₯Ό μž¬μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. (μ„ λ³„μ μœΌλ‘œ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.) class λ₯Ό μ‚¬μš©ν•΄μ„œ κ΅¬ν˜„ν•˜λŠ” κ²ƒκ³Όμ˜ 차이점은 μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€. 상속과 합성이 λͺ¨λ‘ κ΅¬ν˜„κ°€λŠ₯ν•œ class 와 달리, factory function μ—μ„œλŠ” ν•©μ„±μœΌλ‘œ κ΅¬ν˜„ν•΄μ•Ό ν•©λ‹ˆλ‹€.

/** 1. [class] 상속 **/
class Service {
  doSomething(){ console.log("doSomething"); }
}
class SpecialService extends Service {
  doSomethingElse(){ console.log("doSomethingElse"); }  
}
var specialService = new SpecialService();
specialService.doSomething();
specialService.doSomethingElse();

/** 2. [class] ν•©μ„± **/
class Service {
  doSomething(){ console.log("doSomething"); }
}
class SpecialService{
  constructor(args){
    this.service = args.service;
  }
  doSomething() { this.service.doSomething(); } 
  
  doSomethingElse(){ console.log("doSomethingElse"); }
}
var specialService = new SpecialService({
   service : new Service()
});
specialService.doSomething();
specialService.doSomethingElse();

/** 3. [factory function] ν•©μ„± **/
function Service() {
  function doSomething(){ console.log("doSomething"); }
  return Object.freeze({
    doSomething
  });
}
function SpecialService(args){
  var service = args.service;
  function doSomethingElse(){ console.log("doSomethingElse"); }
  return Object.freeze({
    doSomething : service.doSomething,
    doSomethingElse
  });
}
var specialService = SpecialService({
   service : Service()
});
specialService.doSomething();
specialService.doSomethingElse();
5. this

thisλŠ” nested function, callback μ—μ„œ μ»¨ν…μŠ€νŠΈ 상싀 λ¬Έμ œκ°€ λ°œμƒν•  수 μžˆμŠ΅λ‹ˆλ‹€. this λ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ³  ν΄λ‘œμ € + μ§€μ—­λ³€μˆ˜λ₯Ό 톡해 OOP λ₯Ό κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€. (arrow function 으둜 ν•΄κ²°κ°€λŠ₯ν•˜κΈ΄ ν•©λ‹ˆλ‹΅..)

function TodoModel(){
    var todos = [];
        
    function reload(){ 
        setTimeout(function log() { 
           console.log(todos);        //[]
       }, 0);
    }
}
todoModel.reload();                   //[]
$("#btn").click(todoModel.reload);    //[]
6. Immutable API

Γ’bject.freeze() λ₯Ό 톡해 API λ₯Ό μˆ˜μ •ν•  수 μ—†κ²Œ 막을 수 μžˆμŠ΅λ‹ˆλ‹€.


챕터 진행상황

  • 1μž₯ '객체, 섀계'
  • 2μž₯ '객체지ν–₯ ν”„λ‘œκ·Έλž˜λ°'
  • 3μž₯ 'μ—­ν• , μ±…μž„, ν˜‘λ ₯'
  • 4μž₯ '섀계 ν’ˆμ§ˆκ³Ό νŠΈλ ˆμ΄λ“œμ˜€ν”„'
  • 5μž₯ 'μ±…μž„ ν• λ‹Ήν•˜κΈ°' (λ°œν‘œ)
  • 6μž₯ '메세지와 μΈν„°νŽ˜μ΄μŠ€'
  • 7μž₯ '객체 λΆ„ν•΄'
  • 8μž₯ 'μ˜μ‘΄μ„± κ΄€λ¦¬ν•˜κΈ°' (λ°œν‘œ)
  • 9μž₯ 'μœ μ—°ν•œ 섀계'
  • 10μž₯ '상속과 μ½”λ“œ μž¬μ‚¬μš©'
  • 11μž₯ 'ν•©μ„±κ³Ό μœ μ—°ν•œ 섀계'
  • 12μž₯ 'λ‹€ν˜•μ„±'
  • 13μž₯ 'μ„œλΈŒν΄λž˜μ‹±κ³Ό μ„œλΈŒ 타이핑'
  • 14μž₯ '일관성 μžˆλŠ” ν˜‘λ ₯'
  • 15μž₯ 'λ””μžμΈ νŒ¨ν„΄κ³Ό ν”„λ ˆμž„μ›Œν¬'

oop's People

Contributors

aeong98 avatar soobinjung1013 avatar seoyeonkim-gec avatar seoyeonkim01 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.