JS 문법 정리 (4)

2023. 8. 18. 02:22·Programming/JavaScript

0. class, object 

Class

- template

- declare once

- no data in 

- syntactical sugar over prototype-based inheritance

 

Object

- instance of a class

- created many times

- data in

 

1. Class declarations

class Person{
    constructor(name, age){
        this.name = name;
        this.age = age;
    }
    speak(){
        console.log(`${this.name}:hello!`);
    }
}

const cucu = new Person('cucu', 99);
console.log(cucu.age);
console.log(cucu.name);
cucu.speak();

 

2. getter and setters

get : value return

set : value set

class User{
    constructor(firstName, lastName, age){
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
    get age(){
        return this._age;
        // .age로 하는 경우 : full stack
    }
    set age(value){
    this._age = value < 0 ? 0 : value;
    }
}
const user1 = new User('cucu', 'bab', -1);
console.log(user1.age);

 

3. Fields (public, private)

publicField = 1;

#privateField = 0; -> undefined로 호출

 

4. Static

static x → class 명으로 호출 (class 자체에 붙어있기 때문)

static 호출 시, undefined

메모리의 사양을 위하여.

 

5. ☆★상속과 다양성

class Shape{
    constructor(width, height, color){
        this.width = width;
        this.height = height;
        this.color = color;
    }
    draw(){
        console.log(`drawing ${this.color} color of`);
    }
    getArea(){
        return this.width *this.height;
    }
}
class Rectangle extends Shape{}
class Triangle extends Shape{
    getArea(){
    return (this.width *this.height)/2;
}}
const rectangle = new Rectangle(20, 10, 'blue');
rectangle.draw();
const triangle = new Triangle(20, 10, 'pink');
triangle.draw();
console.log(rectangle.getArea());

 

6. instanceOf

return T / F 

console.log(triangle instanceof Shape); // shape을 상속했으니 true
console.log(rectangle instanceof Object); // 모든 object class는 Object 상속함 true

'Programming > JavaScript' 카테고리의 다른 글

JS 문법 정리 (6)  (0) 2023.08.24
JS 문법 정리 (5)  (0) 2023.08.22
JS 문법 정리 (3)  (1) 2023.08.15
JS 문법 정리 (2)  (0) 2023.08.15
JS 문법 정리  (0) 2023.08.14
'Programming/JavaScript' 카테고리의 다른 글
  • JS 문법 정리 (6)
  • JS 문법 정리 (5)
  • JS 문법 정리 (3)
  • JS 문법 정리 (2)
gitit
gitit
짬내서 쓰는 프론트엔드와 기술 메모 공간
  • gitit
    깃잇-gitit
    gitit
  • 전체
    오늘
    어제
    • 분류 전체보기
      • Coding Test
        • Programmers
        • BackJoon
      • Development Tools
        • Firebase
        • Git
        • Monorepo
      • Programming
        • JavaScript
        • React
        • React-Native
      • etc.
        • GeekNews
        • Blockchain
      • Technical Interview
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    알고리즘
    AWS
    백준
    기술 질문
    frontend
    독학
    js
    자바스크립트
    프론트엔드
    BFS
    코딩테스트
    매일메일
    modal
    kakao
    geeknews
    긱뉴스
    리액트
    파이썬
    styled-components
    기본문법
    node.js
    javascript
    Firebase
    파이어베이스
    코테
    프로그래머스
    개발
    코딩
    React
    FE
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
gitit
JS 문법 정리 (4)
상단으로

티스토리툴바