
๐ Parameter properties
ํ์ ์คํฌ๋ฆฝํธ์์ ํด๋์ค์ constructor์์ ์์ฑ์ ์ ์ธํ ๋ ๋งค๊ฐ๋ณ์์์ ์ ์ธํ๊ณ ์ด๊ธฐํํ๋ ๊ฒ์ ํ์ฉํ๋ค.
ํด๋์ค ์ ๊ทผ ์ ์ด์์ ์ฌ์ฉ๋ ํ์ฉํ๋ค.
์์
class Octopus {
readonly name: string;
readonly numberOfLegs: number = 8;
constructor(theName: string) {
this.name = theName;
}
}
๊ธฐ์กด์ ์์ฑ ์์
class Octopus {
readonly numberOfLegs: number = 8;
constructor(readonly name: string) {}
}
constructor ๋งค๊ฐ๋ณ์์ ์์ฑ์ ์ ์ธํ๊ณ ์ด๊ธฐํํ๊ธฐ ๋๋ฌธ์ body ์๋ต๊ฐ๋ฅํจ
JS ์ปดํ์ผ ๊ฒฐ๊ณผ
"use strict";
class Octopus {
constructor(name) {
this.name = name;
this.numberOfLegs = 8;
}
}
let dad = new Octopus("Man with the 8 strong legs");
dad.name;
Reference
https://www.typescriptlang.org/docs/handbook/classes.html#parameter-properties
https://typescript-eslint.io/rules/parameter-properties/#prefer

๐ Parameter properties
ํ์ ์คํฌ๋ฆฝํธ์์ ํด๋์ค์ constructor์์ ์์ฑ์ ์ ์ธํ ๋ ๋งค๊ฐ๋ณ์์์ ์ ์ธํ๊ณ ์ด๊ธฐํํ๋ ๊ฒ์ ํ์ฉํ๋ค.
ํด๋์ค ์ ๊ทผ ์ ์ด์์ ์ฌ์ฉ๋ ํ์ฉํ๋ค.
์์
class Octopus {
readonly name: string;
readonly numberOfLegs: number = 8;
constructor(theName: string) {
this.name = theName;
}
}
๊ธฐ์กด์ ์์ฑ ์์
class Octopus {
readonly numberOfLegs: number = 8;
constructor(readonly name: string) {}
}
constructor ๋งค๊ฐ๋ณ์์ ์์ฑ์ ์ ์ธํ๊ณ ์ด๊ธฐํํ๊ธฐ ๋๋ฌธ์ body ์๋ต๊ฐ๋ฅํจ
JS ์ปดํ์ผ ๊ฒฐ๊ณผ
"use strict";
class Octopus {
constructor(name) {
this.name = name;
this.numberOfLegs = 8;
}
}
let dad = new Octopus("Man with the 8 strong legs");
dad.name;
Reference
https://www.typescriptlang.org/docs/handbook/classes.html#parameter-properties
https://typescript-eslint.io/rules/parameter-properties/#prefer