반응형
// 문법을 엄격하게 봐줌
'use strict';
class Person {
//생성자
constructor(name, age){
this.name = name;
this.age = age;
}
//함수
speak(){
console.log(`${this.name}: hello`);
}
}
const mario = new Person('Mario', 35);
console.log(mario.name);
console.log(mario.age);
mario.speak();
//2. Getter and setters
class User{
constructor(firstName, lastName, age){
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
get age(){
return this._age;
}
set age(value){
// if(value < 0){
// throw Error('age can not be negative');
// }
this._age = value < 0 ? 0 : value;
}
}
const user1 = new User('Steave', 'job', -1);
console.log(user1.age);
//4. Static properties and methods
class Article {
static publisher = 'Dream Coding';
constructor(articleNumber) {
this.articleNumber = articleNumber;
}
static printPublisher(){
console.log(Article.publisher);
}
}
const article1 = new Article(1);
const article2 = new Article(2);
console.log(Article.publisher);
Article.printPublisher();
//5. Inheritance
class Shape {
constructor(width, height, color){
this.width = width;
this.height = height;
this.color = color;
}
draw(){
console.log(`drawing ${this.color} color of`);
}
getArea(){
return this.width * this.height;
}
}
class Rectangle extends Shape {
}
class Triangle extends Shape{
draw(){
super.draw();
console.log(' :) ')
}
getArea(){
return (this.width * this.height) / 2 ;
}
toString(){
return `Triangle Color : ${this.color}`;
}
}
const rectangle = new Rectangle(20, 20, 'blue');
rectangle.draw();
console.log(rectangle.getArea());
const triangle = new Triangle(20, 20, 'red');
triangle.draw();
console.log(triangle.getArea());
console.log(triangle.toString());
반응형
'2020 > Vanilla JavaScript' 카테고리의 다른 글
Vanilla JavaScript # 모달 만들기 (0) | 2020.03.29 |
---|---|
Javascript #prototype (0) | 2020.03.28 |
JavaScript #ES6 Class (0) | 2020.03.28 |
JavaScript #내장함수 reduce (0) | 2020.03.28 |
JavaScript #내장함수 (shift, pop, unshift, push, concat, join) (0) | 2020.03.28 |