问题

编写一个类和类继承,类名为Person,属性name,age,方法sayHello.

一个Student类,继承自Person,自有属性score,自有方法study

(请用ES5和ES6两种技术分别实现)

实现

ES6

//  编写一个类和类继承,类名为Person,属性name,age,方法sayHello.
// 一个Student类,继承自Person,自有属性score,自有方法study
// (请用ES5和ES6两种技术分别实现)

class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}

sayHello() {
console.log('Hello');
}
}

class Student extends Person {
constructor(name, age, score) {
super(name, age);
}

study() {
console.log(this.name);
console.log(this.age);
console.log('i am studing...');
}
}

const stu = new Student('zzh', 18, 100);
stu.sayHello();
stu.study();
//Hello
//zzh
//18
//i am studing...

ES5 (借用构造函数法)

function Person(name, age) {
this.name = name;
this.age = age;

this.sayHello = function() {
console.log('Hello');
}
}

function Student(name, age, score) {
Person.call(this);
this.score = score;
this.study = function() {
console.log('i am studing...');
}
}

const stu = new Student('zzh', 18, 100);
stu.sayHello();
stu.study();

//Hello
//i am studing...