继承
继承
ES6继承 class类
class Parent{ constructor(){ this.name='父亲' } Run(){ Console.log(这里是 Parent.prototype.run(){ }) } } class Children extends Parent{ constructor(){ super(); this.title='儿子' } } var p1 = new Children() console.log(p1.name) //父亲,就可以继承了
原型链继承
function Parent(){ this.age = 19; this.run = function(){} } function Child(){ this.name = '张三' } //原型链 Child.prototype = new Parent(); var o1 = new Child(); var o2 = new Child(); console.log( o1.age ); 19 console.log( o1.run === o2.run ); true
用call继承
function Parent(){ this.age = 18; this.run = function(){} } function Child(){ Parent.call(this); this.name = '张三' } var o1 = new Child(); var o2 = new Child(); console.log( o1.age ); 18 console.log( o1.run === o2.run ); false
本文来自博客园,作者:杨建鑫,转载请注明原文链接:https://www.cnblogs.com/qd-lbxx/p/16258374.html