该函数实现了寄生组合持续的最简单情势。
这个函数接收两个参数,一个子类,一个父类。
第一步创建父类原型的副本,第二步将创建的副本添加constructor属性,第三部将子类的原型指向这个副本。
- function SuperType(name) {
- this.name = name
- this.colors = ['red', 'blue', 'green']
- }
- SuperType.prototype.sayName = function () {
- console.log(this.name)
- }
- function SubType(name, job) {
- // 持续属性
- SuperType.call(this, name)
- this.job = job
- }
- // 持续
- inheritPrototype(SubType, SuperType)
- var instance = new SubType('Jiang', 'student')
- instance.sayName()
弥补:直接应用Object.create来实现,其实就是将膳绫擎封装的函数拆开,如许演示可以更轻易懂得。
- function SuperType(name) {
- this.name = name
- this.colors = ['red', 'blue', 'green']
- }
- SuperType.prototype.sayName = function () {
- console.log(this.name)
- }
- function SubType(name, job) {
- // 持续属性
- SuperType.call(this, name)
- this.job = job
推荐阅读
在本文中,我们将解释 Linux 体系中最关键的治理义务之一——关于体系 / CPU 的负载(load)和平均负载(Load average)的机能监控。起首来看所有的类 UNIX 体系中两个重要的表述:>>>详细阅读
本文标题:JavaScript六种继承方式
地址:http://www.17bianji.com/lsqh/35904.html
1/2 1