드림코딩 자바스크립트 기초강의 정리6

Dongwoo Kim·2021년 7월 16일
0

클래스와 오브젝트의 차이점(class vs object) 객체지향 언어 클래스 정리

0:51 클래스(class): 연관있는 속성들을 묶어놓은 것 field와 method가 있다.

class person{
	name; //fields 속성
	age; //fields 속성
	speak; //method 행동
}

3:08 클래스: 붕어빵 틀, 청사진(template, declare once, no data in)
오브젝트: 붕어빵(instance of a class, created many times, data in)

4:30 클래스 선언:

'use strict';
//Object-oriented programming
//class: template
//Javascript classes
//	-introduced in ES6
//	-syntatical sugar over prototype-based inheritance

//1. Calss declarations
class Person {
	//constructor
  constructor(name, age) {
  	    //fields
    this.name = name;
    this.age = age;
  } 
    //methods
  speak() {
  	console.log(`${this.name}: hello!`);
  }
}

//Object 생성
const ellie = new Person('ellie', 20);
console.log(ellie.name); //ellie
console.log(ellie.age); //20
ellie.speak(); //ellie: hello!

8:28 Getter&Setter:

class User {
	constructor(firstName, lastName, age) {
    	this.firstName = firstName;
    	this.lastName = lastName;
      	this.age = age;
    }
  
  	get age() {
    	return this._age; //callstack exceed 방지
    }
  
  	set age(value) {
      	//if (value < 0) {
         //throw Error('age can not be negative');
        //}
    	this._age = value < 0 ? 0 : value;
    }
 }

const user1 = new User('Steve', 'Jobs', -1);
console.log(user1.age); //-1 -> Wrong!

사용자가 잘못된 값을 넣지 못하게 하는 것

14:31 public&private fields : 생성자(constructor)를 쓰지 않고 fields 사용가능

15:00 static: field값을 construct에 할당하지 않고 클래스에 할당할 수 있게 한다. 호출 시 할당변수명.필드 가 아닌 클래스.필드 로 호출한다.

18:00 extends: 클래스 상속가능, 필요한 메소드 재정의 가능(overriding), 메소드 재활용+변경은 super.메소드 사용

22:00 instanceof 상속 된지 아닌지 확인가능

23:56 mdn reference

profile
水滴石穿

0개의 댓글