09009
[JavaScript] 클래스 본문
클래스
const User = function (name, age) {
this.name = name;
this.age = age;
this.showName = function() {
console.log(this.name);
};
};
const neto = new User("Neto", 28);
class User2 {
constructor(name, age) {
this.name = name;
this.age = age;
}
showName() {
console.log(this.name);
}
}
const toni = new User2("Toni", 19);
neto.showName(); // Neto
toni.showName(); // Toni
위와 같은 결과가 출력됨
const User = function (name, age) {
this.name = name;
this.age = age;
// this.showName = function() {
// console.log(this.name);
// };
};
User.prototype.showName = function() {
console.log(this.name);
}
const neto = new User("Neto", 28);
class User2 {
constructor(name, age) {
this.name = name;
this.age = age;
}
showName() {
console.log(this.name);
}
}
const toni = new User2("Toni", 19);
neto.showName(); // Neto
toni.showName(); // Toni
'Front-End > Javascript' 카테고리의 다른 글
[JavaScript] 콜백 함수 (0) | 2024.05.13 |
---|---|
[JavaScript] this, bind (0) | 2024.05.10 |
[JavaScript] 변수, 스코프, 생성자 함수 (0) | 2024.05.08 |
[JavaScript] 날짜 변환 함수 정리 (0) | 2024.04.09 |
[JS] DOM 조작 기초 (0) | 2023.05.24 |
Comments