// 父类
class People{constructor(name){this.name = name}eat(){console.log(`${this.name} eat something`)}
}// 子类
class Student extends People{constructor(name, number){super(name)this.number = number}sayHi(){console.log(`姓名:${this.name},学号:${this.number}`)}
}class Teacher extends People{constructor(name, major){super(name)this.major = major}teach(){console.log(`${this.name} 教 ${this.major}`)}
}// 实例
const ll = new Student('李雷', 1234)
console.log(ll.name)
console.log(ll.number)
ll.sayHi()
ll.eat()// 实例
const wanglaoshi = new Teacher('王老师', '语文')
console.log(wanglaoshi.name)
console.log(wanglaoshi.major)
wanglaoshi.teach()
wanglaoshi.eat()
效果

console.log(ll instanceof Student) // true
console.log(ll instanceof People) // true
console.log(ll instanceof Object) // trueconsole.log([] instanceof Array) // true
console.log([] instanceof Object) // trueconsole.log({} instanceof Object) // true
实例的隐式原型(__proto__)指向构造函数的显示原型(prototype)。
// class 实际上是函数,可见是语法糖
console.log(typeof People)
console.log(typeof Student)// 隐式原型
console.log(ll.__proto__)
// 显示原型
console.log(Student.prototype)console.log(ll.__proto__ === Student.prototype)

获取属性ll.name或执行方法ll.sayHi()、ll.eat()时,现在自身属性和方法中寻找,如ll.name和ll.sayhi()都在自身属性和方法。如果找不到,则自动去__proto__中查找,如ll.eat()。
// 原型链
console.log(Student.prototype.__proto__)
console.log(People.prototype)
console.log(People.prototype === Student.prototype.__proto__)


手写简易jQuery
一段文字 1一段文字 2一段文字 3

上一篇:当红女演员杨紫,起诉!
下一篇:【笔试强训】Day1