TIL_21.01.28 πŸƒπŸ½β€β™‚οΈπŸƒπŸ½β€β™‚οΈ

Doum KimΒ·2021λ…„ 1μ›” 28일
0

TIL

λͺ©λ‘ 보기
70/71
post-thumbnail

객체와 클래슀

κ³΅μ‹λ¬Έμ„œ μ°Έκ³ 

class Person {
  constructor(protected name: string, private age: number) {
    this.name = name;
    this.age = age;
  }
}

class User extends Person {
  constructor(name: string, age: number) {
    super(name, age);
    console.log(this.name);
    console.log(this.age); // Property 'age' is private and only accessible within class 'Person'.ts(2341)
  }
}
const user = new User("Kim", 20);
console.log(user.name); // Property 'name' is protected and only accessible within class 'Person' and its subclasses.

μ ‘κ·Ό μ œν•œμž

μ ‘κ·Ό κ°€λŠ₯μ„±publicprotectedprivate
클래슀 λ‚΄λΆ€β—―β—―βœ•
μžμ‹ 클래슀 λ‚΄λΆ€β—―β—―βœ•
클래슀 μΈμŠ€ν„΄μŠ€β—―βœ•βœ•

μΈν„°νŽ˜μ΄μŠ€ κ΅¬ν˜„

interface IPerson {
  name: string;
  age?: number;
}

class Person implements IPerson {
  constructor(public name: string, public age?: number) {}
}

const person = new Person("Kim", 20);
console.log(person); // Person {name: "Kim", age: 20, constructor: Object}

interface 생성 ν›„ class에 implements ν‚€μ›Œλ“œ μ‚¬μš©ν•΄μ„œ 적용 constructorμ—μ„œ this.name = name μ΄λ ‡κ²Œ ν•˜μ§€ μ•Šμ•„λ„ λœλ‹€.

readonly

interface IPerson {
  name: string;
  age?: number;
}

class Person implements IPerson {
  constructor(public readonly name: string, public age?: number) {}
}

const person = new Person("Kim", 20);
person.name = "Lee"; // Cannot assign to 'name' because it is a read-only property.ts(2540)

readonly ν‚€μ›Œλ“œ μ΄μš©ν•΄ μƒμˆ˜ 선언을 ν•  수 μžˆλ‹€.

static

interface IPerson {
  name: string;
  age?: number;
}

class Person implements IPerson {
  constructor(public readonly name: string, public age?: number) {}

  static sayHello() {
    console.log("Hello");
  }
}

Person.sayHello(); // Hello

static ν‚€μ›Œλ“œλŠ” 정적 λ©”μ†Œλ“œλ₯Ό μ •μ˜ν•  λ•Œ μ‚¬μš©ν•œλ‹€. 클래슀의 μΈμŠ€ν„΄μŠ€λ‘œ λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜λŠ”κ²Œ μ•„λ‹ˆλΌ 클래슀의 μ΄λ¦„μœΌλ‘œ ν˜ΈμΆœν•œλ‹€.

abstract class

abstract class Person {
  abstract sayHi(): void;
  sayHello(): void {
    console.log("Hello");
  }
}

class User extends Person {
  sayHi() {
    console.log("Hi!");
  }
}

const user = new User();
user.sayHi(); // Hi!
user.sayHello(); // Hello

좔상 ν΄λž˜μŠ€λŠ” 좔상 λ©”μ†Œλ“œμ™€ 일반 λ©”μ†Œλ“œλ₯Ό ν¬ν•¨ν•˜κ³ , λ‚΄μš©μ΄ 없이 λ©”μ†Œλ“œ 이름과 νƒ€μž…λ§Œ μ„ μ–Έλœ λ©”μ†Œλ“œλ₯Ό λœ»ν•œλ‹€. abstract ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€. 직접 μΈμŠ€ν„΄μŠ€ 생성할 수 μ—†μœΌλ©° 상속을 μœ„ν•΄ μ‘΄μž¬ν•œλ‹€. λ”°λΌμ„œ 좔상 클래슀의 μ„œλΈŒ ν΄λž˜μŠ€λŠ” 좔상 λ©”μ†Œλ“œλ₯Ό λ°˜λ“œμ‹œ κ΅¬ν˜„ν•΄μ€˜μ•Όν•œλ‹€.

νƒ€μž… λ³€ν™˜

const person: object = { name: "kim" };

console.log(person.name); // Property 'name' does not exist on type 'object'.ts(2339)

μœ„ μ½”λ“œμ˜ 였λ₯˜λŠ” person의 νƒ€μž…μ΄ object인데 object νƒ€μž…μ΄ name 속성을 가지지 μ•ŠκΈ° λ•Œλ¬Έμ— λ°œμƒν•œλ‹€. λ”°λΌμ„œ 이 문제λ₯Ό νƒ€μž… λ³€ν™˜ ꡬ문을 μ΄μš©ν•΄ ν•΄κ²°ν•  수 μžˆλ‹€.

(<{ name: string }>person).name

person λ³€μˆ˜λ₯Ό μΌμ‹œμ μœΌλ‘œ name 속성이 μžˆλŠ” νƒ€μž…μœΌλ‘œ λ³€ν™˜ν•΄ 속성값을 μ–»κ²Œ ν•œλ‹€.

but

νƒ€μž…μŠ€ν¬λ¦½νŠΈμ—μ„œλŠ” νƒ€μž… λ³€ν™˜μ΄ μ•„λ‹Œ νƒ€μž… 단언 (type assertion) 이라고 ν‘œν˜„ν•œλ‹€.

(<type>Object) λ˜λŠ” Object as type ν˜•νƒœκ°€ μžˆλ‹€.

0개의 λŒ“κΈ€