オブジェクト向け


オブジェクト向けプログラミング


►モデルになるための青写真を作成する(class)
 그 청사진을 바탕으로 한 객체(Object)를 만드는 instance) 프로그래밍 패턴
  • ES 5クラスは、関数
  • として定義することができる.
        function Car (brand, name, color) {
        	//인스턴스가 만들어질 때 실행되는 코드
        }
  • ES 6はまたclassキーワード定義
  • を使用することができる.
        class Car() {
        	constructor(brand, name, color) {
        	//인스턴스가 만들어질 때 실행되는 코드
        	}
        }
    ►newキーワードでクラスのインスタンスを作成できます.
    let avante = new Car('hyundai', 'avante', 'black');
    
    let mini = new Car('bmw', 'mini', 'white');
    
    let beetles = new Car('volkswagen', 'beetles', 'red');
    
    각각의 인스턴스는 Car라는 클래스의 고유한 속성과, 메소드를 갖습니다
    ►属性と方法
    クラスで属性とメソッドを定義し、インスタンスで使用します.
  • プロパティ
  • brand
  • name
  • color
  • currentFuel
  • maxSpeed
  • メソッド
  • refuel()
  • setSpeed()
  • drive()
  • 以上のように,オブジェクト向けプログラミングは現実世界に基づいてプログラミングモデルを作成する際に非常に有用である.
    ►クラス:属性の定義
    ES5
    function Car(brand, name, color) {
    	this.brand = brand;
    	this.name = name;
    	this.color = color;
    }
    
    ES6
    class Car() {
    	constructor(brand, name, color) {
    		this.brand = brand;
    		this.name = name;
    		this.color = color;
    	}
    }
    ►クラス:メソッドの定義
    ES5
     function Car(brand, name, color) {/* 생략 */}
    	Car.prototype.refuel = function() {
    		// 연료 공급을 구현하는 코드
    	}
    
    	Car.prototype.drive = function() {
    		// 운전을 구현하는 코드
    	}
    
    ES6
     class Car() {
    	constructor(brand, name, color) {/* 생략 */}
    	 
    		retuel() {
    		}
    		
    		drive() {
    		}
      }
    ►インスタンスでの使用例
    ES5
    function Car(brand, name, color) {
    	this.brand = brand; //이 예시에서 this는 avante와 mini를 의미함
    	this.name = name;
    	this.color = color;
    }
    
    let avante = new Car('hyundai', 'avante', 'black');
    
    let mini = new Car('bmw', 'mini', 'white');
    
    avante.color // black;
    avante.name // avante;
    
    mini.brand // bmw;
    
    Car.prototype.drive = function () {
    	console.log(this.model + '출발!');
    }
    
    avante.drive(); // avante출발!!
    
    mini.drive(); // mini출발!!
    ► prototype? constructor? this?
  • プロトタイプ:モデル青写真を作成するプロトタイプオブジェクト(元のフォーマット)
  • コンストラクタ:インスタンスの初期化時に実行されるコンストラクタ
  • this:関数が実行されると、各役割ドメインに作成される一意の実行コンテキスト(実行コンテキスト)
    newキーを使用してインスタンスを作成する場合、インスタンスはthis値
  • です.
    ►概要
    // 이 함수는 constructor(생성자)함수
    // Car === class
    function Car(brand, name, color) {
    	this.brand = brand; //this 객체 : 이 예시에서 this는 avante와 mini를 의미함
    	this.name = name;
    	this.color = color;
    } 
    // Car.prototype = prototype 객체 : 여기에 속성이나 메소드를 정의할 수 있음
    Car.prototype.drive = function () {
    	console.log(this.name + '가 운전을 시작합니다');
    }
    //여기서 avante === instance
    let avante = new Car('hyundai', 'avante', 'black');