You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionBase(name,sno){this.name=name;this.sno=sno;}Base.prototype.learing=function(){console.log(this);}functionStudent(name,sno,class_name){Base.call(this,name,sno);// 调用父类的属性this.class_name=class_name;}constst=newStudent('qy','20170102','3-2')console.log(st);st.learing();// Uncaught TypeError: st.learing is not a function // 不存在的原因 Student调用Base的属性,为对方法进行继承,如何实现继承呢?// 初步想法是 使用 Student.prototye = Base.prototype
原型链继承
通过prototype去实现
属性的继承
继承方法
使用 Student.prototye = Base.prototype实现继承方法
这样会出现一个问题,我们在子对象prototype上定义一个方法,会在父类上出现,这不符合面向对象的思想
如何解决play_game在子对象出现,在父对象不出现呢,我们可以通过子实例去解决这个问题,实例如下:
这里还会出现一个问题,在我们打印Student.prototype.constructor, 构造器为父类,应该是自己,我们需要通过手动指定去解决这个问题:
使用Object.create实现继承
这样就实现继承,类似于es6
继承属性和方法
The text was updated successfully, but these errors were encountered: