[Javascript] ์๋ฐ์คํฌ๋ฆฝํธ์์ Class ์ฌ์ฉํ๊ธฐ-constructor, extends, super ์ฌ์ฉ๋ฒ
class Person {
}
let kim = new Person();
console.log(kim);

class๋ก ๋ง๋ค์ด์ค ์์ Person ์ด๋ผ๋ ์ด๋ฆ์ ๊ฐ์ฒด๊ฐ ์์ฑ๋๋ค
Constructor(์์ฑ์)๋ฅผ ์ด์ฉํ๋ฉด class ๊ฐ์ฒด์ ์ด๊ธฐ๊ฐ์ ์ค์ ํด์ค ์ ์๋ค.
class ๋ด๋ถ์์ constructor๋ ํ ๊ฐ๋ง ์กด์ฌํ ์ ์์ผ๋ฉฐ, 2๊ฐ ์ด์ ์ฌ์ฉํ๋ฉด Syntax Error๊ฐ ๋ฐ์ํจ
class Person{
constructor(name,age,city){
this.name = name;
this.age = age;
this.city = city;
}
}
let kim = new Person('Kim', '24', 'Seoul');
console.log(kim);

[JavaScript] ES6 Class์ ์์ฑ์(constructor) ํจ์
๊ฐ์ฒด(์ธ์คํด์ค)๋ฅผ ์์ฑํ๊ณ ํด๋์ค ํ๋๋ฅผ ์ด๊ธฐํํ๊ธฐ ์ํ ํน์ํ ๋ฉ์๋.
โป ํด๋์ค ํ๋ : ์๋ฐ์คํฌ๋ฆฝํธ์ ์์ฑ์ ํจ์์์ this์ ์ถ๊ฐํ ํ๋กํผํฐ
constructor๋ ์๋ก์ด ํด๋์ค๋ฅผ ์์ฑํ ๋ ๊ฐ์ฅ ์ฒ์ ์คํ๋๋ฉฐ ์ด๊ธฐ๊ฐ์ ์ค์ ํด์ค๋ค.
// ํด๋์ค ์ ์ธ๋ฌธ
class Person {
// constructor(์์ฑ์). ์ด๋ฆ์ ๋ฐ๊ฟ ์ ์๋ค.
constructor(name) {
// this๋ ํด๋์ค๊ฐ ์์ฑํ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
// _name์ ํด๋์ค ํ๋์ด๋ค.
this._name = name;
}
}
// ์ธ์คํด์ค ์์ฑ
const me = new Person('Lee');
console.log(me); // Person {_name: "Lee"}
ํด๋์ค๋ constructor๋ผ๋ ์ด๋ฆ์ ๊ฐ์ง ํน๋ณํ ๋ฉ์๋๋ฅผ ํ๋์ฉ๋ง ๊ฐ์ง ์ ์๋ค. 2๊ฐ ์ด์์ผ ์ SyntaxError์ ์ ๋ฐํ๋ค.
์ธ์คํด์ค๋ฅผ ์์ฑํ ๋ new ์ฐ์ฐ์์ ํจ๊ป ํธ์ถํ ๊ฒ์ด ๋ฐ๋ก constructor์ด๋ฉฐ constructor์ ํ๋ผ๋ฏธํฐ์ ์ ๋ฌํ ๊ฐ์ ํด๋์ค ํ๋์ ํ ๋นํ๋ค.
constructor์ ์๋ตํ ์ ์๋ค. ์๋ตํ๋ฉด ํด๋์ค์ constructor(){ }๋ฅผ ํฌํจํ ๊ฒ๊ณผ ๋์ผํ๊ฒ ๋์ํ๋ค.
๋ฐ๋ผ์ ์ธ์คํด์ค์ ํ๋กํผํฐ๋ฅผ ์ถ๊ฐํ๋ ค๋ฉด ์ธ์คํด์ค๋ฅผ ์์ฑํ ์ดํ ํ๋กํผํฐ๋ฅผ ๋์ ์ผ๋ก ์ถ๊ฐํด์ผ ํ๋ค.
class Person { }
const me = new Person();
console.log(me); // Person {}
// ํ๋กํผํฐ ๋์ ํ ๋น ๋ฐ ์ด๊ธฐํ
me._name = 'daeun';
console.log(me); // Person {_name: "daeun"}
constructor๋ ์ธ์คํด์ค์ ์์ฑ๊ณผ ๋์์ ํด๋์ค ํ๋์ ์์ฑ๊ณผ ์ด๊ธฐํ๋ฅผ ์คํํ๋ค.
๋ฐ๋ผ์ ํด๋์ค ํ๋๋ฅผ ์ด๊ธฐํํด์ผ ํ๋ค๋ฉด constructor๋ฅผ ์๋ตํด์๋ ์๋๋ค.
class Person {
constructor(name) {
this._name = name;
}
}
const me = new Person('daeun');
console.log(me);
class์์ ์ค์ ํ ์ด๊ธฐ๊ฐ์ ์ ๊ทผํด ํน์ ๊ธฐ๋ฅ์ ํ๋ ๋ฉ์๋๋ฅผ ๋ง๋๋ ๊ฒ๋ ๊ฐ๋ฅํ๋ค
class ์์ function ํ์์ผ๋ก ๋ง๋ค์ด์ค ๋ค ํด๋น ๋ฉ์๋๋ฅผ ํธ์ถํ๊ธฐ๋ง ํ๋ฉด ๋๋ค
๋ด๋
์ ํด๋น ์ฌ๋์ด ํ ์ด ๋ ๋จน๋๋ค๋ ๋ฉ์๋๋ฅผ class ์์ ์ ์ํ ๋ค ํธ์ถํด๋ณด๋ฉด
class Person{
constructor(name,age,city){
this.name = name;
this.age = age;
this.city = city;
}
//๋ฉ์๋ ์์ฑ
nextYearAge(){
return Number(this.age) + 1;
}
}
let kim = new Person('kim','24','Seoul');
console.log(kim.nextYearAge());

class๋ javascript์ ๊ฐ์ฒด ์ ํํ์ด๋ฏ๋ก ์์ฑ๋ class ๊ฐ์ฒด์ class ๋ฐ์์ ์๋ก์ด ๋ฉ์๋๋ฅผ ๋ฃ๋ ๊ฒ๋ ๊ฐ๋ฅํ๋ค
class Person{
constructor(name,age,city){
this.name = name;
this.age = age;
this.city = city;
}
//๋ฉ์๋ ์์ฑ
nextYearAge(){
return Number(this.age) + 1;
}
}
let kim = new Person('kim','24','Seoul');
kim.eat = function(){
return 'apple'
}
console.log(kim.nextYearAge());
console.log(kim.eat());

ํ์ง๋ง ์ด๋ ๊ฒ ๋ฐ์์ ์ถ๊ฐํ class๋ ๋์ค์ ์๋ก์ด new Person class๋ก ์๋ก์ด ๊ฐ์ฒด๋ฅผ ๋ง๋ค๋๋ ํธ์ถํด์ ์ฌ์ฉํ ์ ์๋ค
class Person {
constructor (name,age,city) {
this.name = name;
this.age = age;
this.city = city;
}
//๋ฉ์๋์์ฑ
nextYearAge() {
return Number(this.age) + 1;
}
}
let kim = new Person('Kim','24','Seoul');
kim.eat = function () {
return 'apple'
}
console.log('๊น์จ ๋ด๋
์๋ ๋ช์ด์ธ๊ฐ์ ?' + kim.nextYearAge());
console.log('๊น์จ๊ฐ ๋จน์๊ฑด? ' + kim.eat());
let park = new Person('Park', '31', 'busan');
console.log('๋ฐ์จ ๋ด๋
์๋ ๋ช์ด์ธ๊ฐ์?' + park.nextYearAge());
console.log('๋ฐ์จ๊ฐ ๋จน์๊ฑด?' + park.eat());
park class์์ eat() ํจ์ ํธ์ถ ์ ์คํ๋์ง ์๋ ๋ชจ์ต
class์์ ์์๊ฐ๋
์ ์ด์ฉํ ์ ์์ต๋๋ค.
class Person {
constructor (name,age,city) {
this.name = name;
this.age = age;
this.city = city;
}
//๋ฉ์๋์์ฑ
nextYearAge() {
return Number(this.age) + 1;
}
}
class introducePerson extends Person {
introduce () {
return `์ ๋ ${this.city}์ ์ฌ๋ ${this.name} ์
๋๋ค.`
}
}
let kim = new introducePerson('Kim','24','Seoul');
console.log(kim.introduce());

introducePersonํด๋์ค์์ Person์ ์์๋ฐ์๊ธฐ ๋๋ฌธ์
this.city์ this.name์ ๋ชจ๋ ์ฌ์ฉํ ์ ์๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
introducePerson ํ์ํด๋์ค์์ ๊ธฐ์กด class์ ๊ฐ์ ๊ฐ์ ธ๋ค ์ฐ๋ ๊ฑด ์ข์์ง๋ง,
์ถ๊ฐ์ ์ผ๋ก introducePerson์ด๋ผ๋ ํ์ ํด๋์ค์์๋ง ์ฌ์ฉํ๊ณ ์ถ์ ๊ฐ์ด ์์ ์ ์๋ค.
์ด ๋ ์ด์ฉํ๋ ๊ฒ์ด super๋ผ๋ ํค์๋์ด๋ฉฐ,
์ด๋ ๊ฐ์ฒด์ ๋ถ๋ชจ๊ฐ ๊ฐ์ง๊ณ ์๋ ๋ฉ์๋๋ฅผ ํธ์ถํ ์ ์๋ค.
์์ ์ชฝ์ ์ถ๊ฐ์ ์ผ๋ก ์ฌ์ฉํ ์ด๊ธฐ๊ฐ์ด ํ์ํ ๊ฒฝ์ฐ constructor์ super๋ก ๋ถ๋ชจ ์ด๊ธฐ๊ฐ์ ์ธํ
ํ ๋ค ์์ class์์๋ง ์ฌ์ฉํ ์ด๊ธฐ๊ฐ๋ง ๋ฐ๋ก ์ง์ ํ๋ ๊ฒ๋ ๊ฐ๋ฅํ๋ฉฐ super ๊ธฐ๋ฅ์ ์ด์ฉํด์ ์์ class์์ ๋ถ๋ชจ ๋ฉ์๋๋ฅผ ํธ์ถํ ์๋ ์๋ค.
class Person {
constructor (name,age, city) {
this.name = name;
this.age = age;
this.city = city;
}
//๋ฉ์๋์์ฑ
nextYearAge() {
return Number(this.age) + 1;
}
}
class introducePerson extends Person {
constructor(name, age, city, futureHope) {
super(name, age, city);
this.futureHope = futureHope
}
introduce () {
return `์ ๋ ${this.city}์ ์ฌ๋ ${this.name} ์
๋๋ค.
๋ด๋
์ ${super.nextYearAge()}์ด์ด๋ฉฐ,
์ฅ๋ํฌ๋ง์ ${this.futureHope} ์
๋๋ค.`
}
}
let kim = new introducePerson('Kim','24','Seoul', '๊ฐ๋ฐ์');
console.log(kim.introduce());

class๋ฅผ ์ด์ฉํ ๊ฒฝ์ฐ ๊ท์น์ฑ์ ๊ฐ๋ ๊ฐ์ฒด๋ฅผ ์ผ๊ด์ฑ ์๊ฒ ๋ง๋๋ ๊ฒ ๊ฐ๋ฅํ๋ฉฐ,
์์์ ํตํด์ ๊ธฐ๋ฅ ํ์ฅ์ด ์ฉ์ดํ๋ค๋ ๊ฒ ์ ์ ์์๋ค.