function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.sayhello = function() {
console.log('hello');
};
function Student(grade) {
this.grade = grade;
}
Student.prototype = new Person('zhangsan', 18);
let test = new Student(3);
console.log(test.grade);
test.sayhello();
console.log(test.name);
console.log(test.constructor);
function Student2(name, age, grade) {
Person.call(this, name, age);
this.grade = grade;
}
let test2 = new Student2('lisi', 19, 4);
console.log(test2.name);
console.log(test2.grade);
function Student3(name, age, grade) {
Person.call(this, name, age);
this.grade = grade;
}
Student3.prototype = Object.create(Person.prototype);
Student3.prototype.constructor = Student3;
let test3 = new Student3('wangwu', 20, 5);
test3.sayhello();
console.log(test3 instanceof Person, test3 instanceof Student3)
console.log(test3.constructor);
console.log(test3.__proto__);
console.log(Student3.__proto__ === Function.prototype);
class Animal {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayhello() {
console.log('hello');
}
}
class Dog extends Animal {
constructor(name, age, color) {
super(name, age);
this.color = color;
}
}
let dog1 = new Dog('maomao', 4, 'yellow');
dog1.sayhello;
console.log(dog1.name);
console.log(Dog.__proto__ === Animal);