发布时间:2025-12-10 23:48:29 浏览次数:14
可用以下两种方法实现继承
class/extendsclassAnimal{
constructor(name){
this.name=name
}
hello(){
console.log('hello')
}
}
classDogextendsAnimal{
constructor(name,say){
super(name)
this.say=say
}
}function/newfunctionAnimal(name){
this.name=name
}
Animal.prototype.hello=()=>{
console.log('hello')
}
functionDog(name,say){
//01 继承属性
Animal.call(this,name)
this.say=say
}
//02 通过连接原型链完成继承
Dog.prototype=Object.create(Animal.prototype)
//03 再加上constructor
Dog.prototype.constructor=Dog
//Reflect.defineProperty(Dog.prototype,"constructor",{
//value:Dog,
//enumerable:false,//不可枚举
//writable:true
//})
到此,关于“怎么在 js 中如何实现继承”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注本站网站,小编会继续努力为大家带来更多实用的文章!