js是一个基于原型的语言
class
可以使用constructor添加一个属性和方法
class Student{
constructor(name,age){
this.name=name
this.age=age
}
sayHi(){
console.log(
`姓名${this.anme} 学号${this.age}`
)
}
}
const xiaoming=new Student("小米",10)
console.log(xiaoming.name)//来获取值
继承
主要使用了
extends实现了继承
super来继承父类的构造函数
扩展和重写方法
class People{//父类
constructor(name){
this.name=name
}
eat(){
console.log(
`${this.anme} eat sonmething`
)
}
}
class Student extends People{//字类
constructor(name,number){
super(name)
this.number=number
}
}
instanceof判断
- instanceo()if操作符 判断 new 关键字创建的引用数据类型 返回值是boolean值
不考虑 null 和 undefined(这两个比较特殊)以对象字面量创建的基本数据类型。
原型
JavaScript 的所有对象中都包含了一个 [proto] 内部属性,这个属性所对应的就是自身的原型JavaScri
[proto] 之外,还有 prototype 属性,当函数对象作为构造函数创建实例时,该 prototype 属性值将被作为实例对象的原型 [proto]
原型链的概念
当一个对象调用自身不存在的属性/方法时,就会去自己 [proto] 关联的前辈 prototype 对象上去找,如果没找到,就会去该 prototype 原型 [proto] 关联的前辈 prototype 去找。依次类推,直到找到属性/方法或 undefined 为止。从而形成了所谓的“原型链”。