[TIL]21.06.14


👨‍💻 今日習ったこと

  • class, super, extend, require, module.exports, prototype, __proto__
  • 1. class
    JavaScriptのクラス宣言にはEs 5とEs 6のバージョンがあります.
    1)Es5
      function Computer(Modelname, brand, color){
      	this.Modelname = Modelname;
      	this.brand = brand;
      	this.color = color;
      }
      
      Computer.prototype.onPower = function(){
      	// 전원을 키는 코드
      }
      Computer.prototype.offPower = function(){
      	// 전원을 끄는 코드
      }
      
    2) Es6
    class Computer{
    	constructor(Modelname, brand, color){
    		this.Modelname = Modelname;
    		this.brand = brand;
    		this.color = color;
    	}
    	onPower(){
    		// 전원을 키는 코드
    	}
    	offPower(){
    		// 전원을 끄는 코드
    	}
    }
    2. super, extends, require, module.exports
    superはextends継承クラスを使用した後、継承データを書き込むためのキーワードです.ではextendsはクラスを継承するキーワードであるべきです.
    requireは、継承したいクラスをロードするキーワード、moduleです.exportsは、クラスを他のクラスにエクスポートする方法です.
    // Animals.js
    
    class Animals{
    	constructor(name, age, whatAnimalLikes){
        	this.name = name;
          	this.age = age;
          	this.whatAnimalLikes = 'feed';
        }
      	eat(){
        	console.log(`${name}가 먹이를 먹습니다.`);
        }
      	sleep(){
        	console.log(`${name}가 잠을 잡니다.`);
        }
      	likeAnimal(){
        	console.log(`${name}${whatAnimalLikes}을 받고 좋아합니다.`);
        }
    }
    
    module.exports = Animals
    
    // Cat.js
    const Animals = require('./Animals');	// 같은 폴더에 있다는 가정하
    
    class Cat extends Animals{
    	constructor(name, age, whatAnimalLikes){
        	super(whatAnimalLikes);	// 부모클래스에서 whatAnimalLikes 를 상속받는 코드
          	this.name = '고양이';
          	this.agre = 3;
        }
        eat(){
        	return super.eat();	// 부모클래스에서 메소드 eat()을 상속받는 코드
        }
        likeAnimal(){
      		return console.log("고양이가 좋아합니다.")	// 부모클래스에서 상속받지않고 메소드를 선언한 코드
        }
    }
    3. prototype, __proto__
    classを宣言すると、このclassはprototypeという名前のオブジェクトを示すことができます.またprototypeという名前のオブジェクトには、そのプロトタイプを示す構造関数があります.
    クラスがクラスを継承している場合、クラスも同様にクラスが継承しているprototypeというオブジェクトを指します.
    ただし、継承されたclassはprototypeオブジェクトではなくprotoオブジェクトを使用してprototypeを指示します.したがって、次の継承クラスのプロトタイプと継承クラスのprotoが同じプロトタイプを参照しているかどうかを確認できます.
    	Animals.prototype === Cat.__proto__;
    	// true
    では、原型という対象には何があるのでしょうか.
    親クラスで宣言されたメソッドと前述したコンストラクション関数があります.
    サラダ烏図生活コード.