成都创新互联网站制作重庆分公司

怎么在js中如何实现继承

这篇文章主要介绍“怎么在 js 中如何实现继承”,在日常操作中,相信很多人在怎么在 js 中如何实现继承问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么在 js 中如何实现继承”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

创新互联公司云计算的互联网服务提供商,拥有超过13年的服务器租用、成都移动机房、云服务器、虚拟主机、网站系统开发经验,已先后获得国家工业和信息化部颁发的互联网数据中心业务许可证。专业提供云主机、虚拟主机、空间域名、VPS主机、云服务器、香港云服务器、免备案服务器等。


可用以下两种方法实现继承

 

class/extends

class Animal {
  constructor (name) {
    this.name = name
  }

  hello () {
    console.log('hello')
  }
}

class Dog extends Animal {
  constructor (name, say) {
    super(name)
    this.say = say
  }
}

function/new

function Animal (name) {
  this.name = name
}

Animal.prototype.hello = () => {
  console.log('hello')
}

function Dog (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 中如何实现继承”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


本文标题:怎么在js中如何实现继承
标题路径:http://cxhlcq.com/article/josjei.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部