原型与原型链是js
js是基于原型继承的语言
1.原型概念:jsvascript都包含了一个proto内部属性,这个属性对应的是自身
除了原型proto之外,还有prototype属性,当函数对象作为构造函数创建实例时,
prototype属性值将被作为实例对象的原型proto
2.原型链当一个对象调用自身不存在的对象或者方法就会去自己proto关联的前辈prototype对象上去找,
如果没有找到,就会去prototype原型proto关联的前辈prototype,
依次类推直到undefined,这就是原型链
3.总结:JavaScript中的对象都有一个内置属性prototype,找不到就会在当前对象的原型对象中接着找,直到找到为最顶层没有的话就结束查找返回undefined
隐式原型:用来构成原型链,实现基于原型的继承
显示原型:用来实现原型的继承与属性共享
- 如何准确判断一个变量是不是数组?
- 手写一个简易的jQuery,考虑插件和扩展性
- class的原型本质,怎么理解?
知识点
- class和继承
- 类型判断instanceof
- 原型和原型链
1.class
构建:construction 有属性、方法
//类
class Student{ //属性
constructor(name,number){
this.name=name
this.number=number
}
sayHi(){ //方法
console.log(
`姓名${this.name},学号${this.number}`
)
}
}
//通过类 new 对象/实例
const huahua = new Student('婳婳',100)
console.log(huahua.name)
console.log(huahua.number)
huahua.sayHi()
继承
- extends
- super
- 扩展或重写方法
//父类
class People{
constructor(name){
this.name=name
}
eat(){
console.log(`${this.name}eat something`)
}
}
//父类
class Student extends People{
construction(name,number){
super(name)
this.number=number
}
sayHi(){
console.log(`姓名${this.name}学号${this.number}`)
}
}
const huahua = new Student('婳婳',100)
console.log(huahua.name)
console.log(huahua.number)
huahua.sayHi()
huahua.eat()
待更新……