Untitled
unknown
plain_text
4 years ago
2.5 kB
7
Indexable
class User {
constructor(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
salary() {
return Math.random() * 1000;
}
get date() {
return (new Date()).getFullYear() - this.age;
}
get name() {
return `${this.firstName} ${this.lastName}`;
}
set name(value) {
this.firstName = value.split(' ')[0];
this.lastName = value.split(' ')[1];
}
}
class Boss extends User{
car;
#personalBonus = 1000;
constructor(firstName, lastName, age) {
super(firstName, lastName, age)
this.isGeneral = true;
}
salary() {
return super.salary() + this.bonus() + this.#personalBonus;
}
bonus(){
return 1000000;
}
}
const user1 = new User('user1', 'petrovich', 200);
const boss1 = new Boss('Ivan', 'Ivanovich', 30);
user1.name = 'User2 LastName';
boss1.car = 'Audi';
console.log(boss1);
//Student //firstName, lastName, rating[], average()
// Teacher students[]; get average
class User {
firstName;
lastName;
static MAX_AGE = 100;
constructor(firstName, lastName){
this.firstName = firstName;
this.lastName = lastName;
}
get name(){
return this.firstName + ' ' + this.lastName;
}
static maxAge(){
return this.MAX_AGE;
}
}
class Teacher extends User{
students;
constructor(firstName, lastName, students) {
super(firstName, lastName);
this.students = students;
}
average(){
return this.students.reduce((acc, s) =>acc + s.average(), 0)/this.students.length;
}
show(){
this.students.forEach(s => console.log(s.name));
}
}
class Student extends User{
rating;
constructor(firstName, lastName, rating) {
super(firstName,lastName)
this.rating = rating;
}
average(){
return this.rating.reduce((a,b)=> a+b)/this.rating.length;
}
addRating(rating){
this.rating.push(rating);
}
}
const student1 = new Student('s1', 's1', [1,2,3,4,5]);
const student2 = new Student('s2', 's2', [10,20,30,4,5]);
const student3 = new Student('s3', 's3', [1,20,3,4,5]);
const teacher = new Teacher('t1', 't2', [student1, student2, student3]);
teacher.show()
console.log(User.maxAge())
Editor is loading...