1. Advanced Objects Introduction

: 향상된 객체에 대한 소개

이 파트에서는,

const robot = {
  model: 'B-4MI',
  mobile: true,
  greetMaster() {
  	console.log(`I'm model ${this.model}, how may I be of service?`);
  }
}

const massProdRobot = (model, mobile) => {
  return {
    model,
    mobile,
    greetMaster() {
      console.log(`I'm model ${this.model}, how may I be of service?`);
    }
  }
}

const shinyNewRobot = massProdRobot('TrayHax', true)

const chargingStation = {
  _name: 'Electrons-R-Us',
  _robotCapacity: 120,
  _active: true,
  _chargingRooms: ['Low N Slow', 'Middle of the Road', 'In and Output'],

  set robotCapacity(newCapacity) {
    if (typeof newCapacity === 'number') {
      this._robotCapacity = newCapacity;
    } else {
      console.log(`Change ${newCapacity} to a number.`)
    }
  },
  get robotCapacity() {
    return this._robotCapacity;
  }
}

2. The 'this' Keyword

: 'this' 키워드

객체는 데이터와 기능의 모음이다. 해당 기능을 객체에 저장한다.

const goat = {
  dietType: 'herbivore',
  makeSound() {
    console.log('baaa');
  }
};

goat 객체에는 makeSound() 메소드가 있다.

goat을 통해서, makeSound() 메소드를 실행시킬 수 있다.

goat.makeSound(); // Prints baaa

goat 객체에 dietType 을 출력하는 새로운 메소드를 추가해보자.

const goat = {
  dietType: 'herbivore',
  makeSound() {
    console.log('baaa');
  },
  diet() {
    console.log(dietType);
  }
};
goat.diet(); 
// Output will be "ReferenceError: dietType is not defined"

이렇게 코드를 작성했을 때, 문제가 발생한다.