const User = function (name, age) {
this.name = name;
this.age = age;
this.showName = function () {
console.log(this.name);
};
};
const mike = new User(โMikeโ, 30);
console.log(mike); // { name: โMikeโ, age: 30, showName: [Function(ananymous)] }
class User2 {
constructor (name, age) {
this.name = name;
this.age = age;
}
showName() {
console.log(this.name);
}
}
const tom = new User2(โTomโ, 19);
console.log(tom); { name: โTomโ, age: 19 }
// ์ ์์ ์ ๋ฌ๋ฆฌ class ์์ ๊ฐ์ฒด๋ฅผ ์ฐ์ด๋ณด๋ฉด ๋ฉ์๋๋ ๋ณด์ด์ง ์์
new
๋ก ํธ์ถ ํ์๋ ๋ด๋ถ์ ์ ์๋ ๋ด์ฉ์ผ๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๊ฒ์ ๋์ผํ๋ค.class
ํค์๋์ constructor(์์ฑ์)
๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.showName()
์ฒ๋ผ class
๋ด์ ์ ์ํ ๋ฉ์๋๋ User2
ํ๋กํ ํ์
(์์๊ฐ์ฒด)์ ์ ์ฅ ๋๋ค.โmike
๋ ๊ฐ์ฒด ๋ด๋ถ์ showName()
์ด ์๋ค.tom
์ ํ๋กํ ํ์
๋ด๋ถ์ showName()
์ด ์๋ค.mike.showName()
, tom.showName()
์ผ๋ก ๋์ผํ๋ค.const User3 = function (name, age) {
this.name = name;
this.age = age;
}
User3.prototype.showName = function () {
console.log(this.name);
}
const jimmy = new User3 (โJimmyโ, 25);
console.log(jimmy); // { name: โJimmyโ, age: 25 }
// ์์๊ฐ์ด ์์ฑ ์ class์ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ฉ์๋๊ฐ prototype์ผ๋ก ํ ๋น๋๋ค.
// ์์ฑ์ ํจ์
const User = function (name, age) {
this.name = name;
this.age = age;
this.showName = function () {
console.log(this.name);
};
};
const mike = User(โMikeโ, 30);
console.log(mike); // undefined
๐ new ๋ฅผ ๋นผ๊ณ ํธ์ถ์ undefined
๋ฅผ ๋ฐํํ๋ค. (์๋ฌ๊ฐ ์๋จ)
// class
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
showName() {
console.log(this.name);
}
}
const eddy = User(โEddyโ, 17); // TypeError
๐ new
๋ฅผ ๋นผ๊ณ ํธ์ถ์ TypeError
๋ฅผ ๋ฐํํจ (์๋ฌ ๋ฐ์)
์ด๋ ์ธ์ ์ค์๋ก ์ธํ ์ํ์ง ์๋ ๋์์ ๋ฐฉ์ง ํ ์ ์๋ค.
// ๋ฐ๋ก ์ ์์ ์ฝ๋์ ์ด์ด์
for (let key in eddy) {
console.log(key);
}
// name, age
๐ class
๋ด๋ถ์ ์ ์ธํ ๋ฉ์๋๋ for..in๋ฌธ์์ ์ ์ธ๋๋ค.
๐ ์์ฑ์ ํจ์์์๋ ๊ฐ์ฒด๊ฐ ๊ฐ์ง๊ณ ์๋ ํ๋กํผํฐ๋ง ํ์ธ ํ๊ธฐ ์ํด hasOwnProperty()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ผ๋ง ํ์ง๋ง class๋ ๊ทธ๋ด ํ์๊ฐ ์๋ค.
์์ฑ์ ํจ์๋ prototype์ผ๋ก ์์ ๊ตฌํ
class
๋ extend
ํค์๋๋ฅผ ์ฌ์ฉ
class Car {
constructor(color) {
this.color = color;
this.wheels = 4;
}
drive () {
console.log(โdriveโฆโ);
}
}
class Bmw extend Car {
stop () {
console.log(โstop!โ);
}
}
const x5 = new Bmw(โblueโ);
console.log(x5); // { color: โblueโ, wheels: 4 }
x5.drive(); // โdriveโฆโ
x5.stop(); // โstop!โ
์ ์ฝ๋๋ฅผ ๋ณด๋ฉด Car
๋ฅผ ์์ ํด๋์ค๋ก ๋ณด๊ณ ์ด๋ฅผ ์์ ๋ฐ์ Bmw
ํด๋์ค๊ฐ ์์ฑ์ ์ญํ ์ ํ์ฌ x5
๋ฅผ ์์ฑํ๋ค.
๋ถ๋ชจ ํด๋์ค์ ํ๋กํผํฐ๋ ๋ฉ์๋๋ฅผ ์์๋ฐ์ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค.
drive()
๋ฅผ ํธ์ถํ๋ค๊ณ ํ๋ฉด ์ฐ์ x5
๊ฐ์ฒด์์ ์ฐพ๊ณ ์์ผ๋ฉด prototype
์์ ์ฐพ๊ณ ์ฌ๊ธฐ์๋ ์์ผ๋ฉด ๋ถ๋ชจ ํด๋์ค์ prototype
์์ ์ฐพ๋๋ค.
์์๋ ์ค๋ช
ํ๋ฏ์ด class
ํค์๋๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ฉ์๋๋ฅผ ํ๋กํ ํ์
์ ์ ์ฅํ๋ค.
๐์ด๋ฏธ์ง ์ถ์ฒ : ์ฝ๋ฉ์๋ง ์ ํ๋ธ
์ ์ด๋ฏธ์ง์์ ๋ณด๋ฏ์ด(๊ฐ์ฒด์ ํ๋กํผํฐ์ ์ด๋ฆ์ ๋ค๋ฆ) ์์ฑ๋ ๊ฐ์ฒด์ ํ๋กํ ํ์ ์ผ๋ก ๋ฉ์๋๊ฐ ๋ค์ด๊ฐ์๊ณ ๊ทธ ํ๋กํ ํ์ ์ผ๋ก ์ต์์ ๊ฐ์ฒด์ ๋ฉ์๋๊ฐ ๋ค์ด๊ฐ ์๋ค. (์ค๋ช ์ ์ด๋ป๊ฒ ํด์ผํ ์ง ๋ชจ๋ฅด๊ฒ ๋ค)
class Car {
constructor(color){
this.color = color;
}
drive() {
console.log(โdriveโฆโ);
}
stop() {
console.log(โstop!โ);
}
}
class Audi extend Car {
stop() {
console.log(โOFFโ);
}
}
const a8 = new Audi (โgrayโ);
a8.stop(); // OFF
๐ ๋ง์ฝ ๋ถ๋ชจ์ ๋ฉ์๋๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํ๋ฉด์ ํ์ฅ ํ๊ณ ์ถ๋ค๋ฉด super
๋ผ๋ ํค์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
class Car {
constructor(color){
this.color = color;
}
drive() {
console.log(โdriveโฆโ);
}
stop() {
console.log(โstop!โ);
}
}
class Audi extend Car {
stop() {
super.stop(); // ์ด ๋ถ๋ถ ์ฝ๋๊ฐ ์ถ๊ฐ๋จ
console.log(โOFFโ);
}
}
const a8 = new Audi (โgrayโ);
a8.stop(); // โstop!โ โOFFโ
super
์ ๋์ผํ ์ด๋ฆ์ ๋ฉ์๋stop()
์ ํธ์ถํ๋ฉด ๋ฎ์ด ์์ฐ์ง ์๊ณ ํ์ฅ์ด ๊ฐ๋ฅํ๋ค.class Car {
constructor(color) {
this.color = color;
}
drive() {
console.log(โdriveโฆโ);
}
}
class Benz extends Car {
this.navigation = 1;
}
const sclass = new Benz (white);
console.log(sclass); // error
constructor
๊ฐ ์๋ค.error
๊ฐ ๋ฐ์ํ๋ค.
class Car {
constructor(color) {
this.color = color;
}
drive() {
console.log(โdriveโฆโ);
}
}
class Benz extends Car {
constructor() { // constructor ์ถ๊ฐ
super(); // super ํค์๋ ์ถ๊ฐ
this.navigation = 1;
}
}
const sclass = new Benz (white);
console.log(sclass); // { color: undefined, navigation: 1 }
constructor
๋ฐ super()
๋ฉ์๋๊ฐ ์ถ๊ฐ ๋์๋ค.color
๊ฐ์ด undefined
๋ก ์ฐํ๋ค.
class Car {
constructor(color) {
this.color = color;
}
drive() {
console.log(โdriveโฆโ);
}
}
class Benz extends Car {
constructor(color) { // ์ธ์ ์ถ๊ฐ
super(color); // ์ธ์ ์ถ๊ฐ
this.navigation = 1;
}
}
const sclass = new Benz (white);
console.log(sclass); // { color: white, navigation: 1 }
constructor
์ color
๋ฅผ ์ ๋ฌํด ์ฃผ๋ ์ ์์ ์ผ๋ก ์ถ๋ ฅ๋๋ค.