Design-loving front-end engineer
Ryong
Design-loving front-end engineer
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
    • Framework
    • React
      • Concept
      • Library
      • Hook
      • Component
      • Test
    • NodeJS
    • Android
      • Concept
      • Code
      • Sunflower
      • Etc
    • Flutter
      • Concept
      • Package
    • Web
    • Web
    • CSS
    • Language
    • JavaScript
    • TypeScript
    • Kotlin
    • Dart
    • Algorithm
    • Data Structure
    • Programmers
    • Management
    • Git
    • Editor
    • VSCode
    • Knowledge
    • Voice
Design-loving front-end engineer

Ryong

JavaScript

[ JS ] Classes

2022. 7. 6. 14:59

๐Ÿ”ด  Introduction to Classes

 
class User {
  constructor(name) {
    this.username = name;
  }
  sayHello() {
    console.log(`Hello, my name is ${this.username}`);
  }
}

const myUser = new User("Ryong");

myUser.sayHello();  // Hello, my name is Ryong

 

๐ŸŸ   Extending Classes

 
class User {
  // this => User
  constructor(name, lastName, email, password) {
    this.username = name;
    this.lastName = lastName;
    this.email = email;
    this.password = password;
  }
  sayHello() {
    console.log(`Hello, my name is ${this.username}`);
  }
  getProfile() {
    console.log(`${this.username} ${this.email} ${this.password}`);
  }
  updatePassword(newPassword, currentPassword) {
    if (currentPassword === this.password) {
      this.password = newPassword;
    } else {
      console.log("Can't change password.");
    }
  }
}

class Admin extends User {
  constructor(superAdmin) {
    this.superadmin = superAdmin;
    // ์ด๋Ÿด ๊ฒฝ์šฐ, ๊ธฐ์กด์˜ constructor๋ฅผ ์žƒ์–ด๋ฒ„๋ฆฌ๊ฒŒ ๋œ๋‹ค.
  }
  deleteWebsite() {
    console.log("Deleting the whole website...");
  }
}

const myAdmin = new Admin("Ryong", "Jeong", "ryong@com", "1234", true);

myAdmin.deleteWebsite();  // Deleting the whole website...

console.log(myAdmin);

 

๐ŸŸก  super

 
class User {
  // this => User
  constructor({ username, lastName, email, password }) {
    this.username = name;
    this.lastName = lastName;
    this.email = email;
    this.password = password;
  }
  sayHello() {
    console.log(`Hello, my name is ${this.username}`);
  }
  getProfile() {
    console.log(`${this.username} ${this.email} ${this.password}`);
  }
  updatePassword(newPassword, currentPassword) {
    if (currentPassword === this.password) {
      this.password = newPassword;
    } else {
      console.log("Can't change password.");
    }
  }
}

const myUser = new User({
  username: "Ryong",
  lastName: "Jeong",
  email: "ryong@com",
  password: "1234"
});

class Admin extends User {
  constructor({ username, lastName, email, password, superAdmin, isAdmin }) {
    super({ username, lastName, email, password });
    this.superAdmin = superAdmin;
    this.isActive = isActive;
  }
  deleteWebsite() {
    console.log("Deleting the whole website...");
  }
}

const myAdmin = new Admin({
  username: "Ryong",
  lastName: "Jeong",
  email: "ryong@com",
  password: "1234",
  superAdmin: true,
  isActive: true
});
 
class Counter {
  constructor({ initialNumber = 0, couterId, plusId, minusId }) {
    this.count = initialNumber;
    this.counter = document.getElementById(counterId);
    this.counter.innerText = initialNumber;
    this.plusBtn = document.getElementByID(plusId);
    this.minusBtn = document.getElementById(minusId);
    this.addEventListeners();
  }
  addEventListeners = () => {
    this.plusBtn.addEventListener("click", this.increase);
    this.minusBtn.addEventListener("click", this.decrease);
  };
  increase = () => {
    this.count = this.count + 1;
    this.repaintCount();
    // arrow function์ด ์•„๋‹Œ normal function์—์„œ this๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ,
    // eventListener ์•ˆ์— ์žˆ๋Š” ํ•จ์ˆ˜์˜ this์ด๊ธฐ ๋•Œ๋ฌธ์—
    // this๋Š” plusBtn์„ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋œ๋‹ค.
    // class ๋‚ด๋ถ€์˜ ๋ชจ๋“  ๊ณณ์—์„œ this๋กœ class๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ธฐ ์œ„ํ•ด์„œ๋Š”
    // arrow function์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹คโ—
  };
  decrease = () => {
    this.count = this.count - 1;
    this.repaintCount();
  };
  repaintCount = () => {
    this.counter.innerText = this.count;
  };
}

new Counter({ counterId: "count", plusId: "add", minusId: "minus" });
์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'JavaScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[ JS ] Generators and Maps  (0) 2022.07.06
[ JS ] Symbol, Set and Map  (0) 2022.07.06
[ JS ] Async / Await  (0) 2022.07.06
[ JS ] Promises  (0) 2022.07.06
[ JS ] For Loop  (0) 2022.07.05
    'JavaScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • [ JS ] Generators and Maps
    • [ JS ] Symbol, Set and Map
    • [ JS ] Async / Await
    • [ JS ] Promises
    Design-loving front-end engineer
    Design-loving front-end engineer
    ๋””์ž์ธ์— ๊ด€์‹ฌ์ด ๋งŽ์€ ๋ชจ๋ฐ”์ผ ์•ฑ ์—”์ง€๋‹ˆ์–ด Ryong์ž…๋‹ˆ๋‹ค.

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”