class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
speak() {
console.log(`$(this.name): hello!`);
}
};
const jiyaho = new Person('jiyaho', 20);
console.log(jiyaho.name);
console.log(jiyaho.age);
jiyaho.speak();
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;
}
}
const user1 = new User('Steve', 'Jobs' -1);
console.log(user1.age);
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 width * this.height;
}
}
class Rectangle extends Shape {}
const rectangle = new Rectangle(20, 20, 'blue');
rectangle.draw();
console.log(rectangle.getArea());
class Triangle extends Shape {
getArea() {
return (width * this.height) / 2;
};
draw() {
super.draw();
console.log('triangle!!');
};
};
const triangle = new Rectangle(20, 20, 'red');
triangle.draw();
console.log(triangle.getArea());