思考题
题目 1. 如何判断一个变量是不是数组?
[] instanceof Array --> true
[] instanceof Object --> true
{} instanceof Object --> true
题目 2. 手写一个简易的jQuery,考虑插件和扩展性
题目 3. class的原型本质怎么理解
知识点
1. class和继承
声明一个类
class Student {
constructor(name,number) {
this.name = name;
this.number = number;
}
sayHi() {
console.log(`姓名:${this.name} , 学号 ${this.number}`)
}
}
通过类声明对象/实例
const xialuo = new Student("夏洛",100);
console.log(xialuo.name,xialuo.number)
xialuo.sayHi();
继承语法
父类
class People {
constructor (name) {
this.name = name;
}
eat() {
console.log(`${this.name} 在吃饭`)
}
}
子类
class Student extends People
constructor(name,number) {
super(name)
this.number = number
}
sayHi() {
console.log(`$(this,name) 打了招呼`)
}
}
2. 类型判断instanceof
[] instanceof Array --> true
[] instanceof Object --> true
{} instanceof Object --> true
3. 原型和原型链
原型关系
· 每个class都有显示原型prototype
· 每个实例都有隐式原型__proto__
· 实例的__proto__指向对应class的prototype
基于原型的执行规则
· 获取属性xialuo.name 或执行方法 xialuo.sayHi() 时
1.先在自身属性和方法寻找
2.如果找不到则自动去__proto__中查找