大众迈腾

1.js的继承的实现方法

大憨熊 提交于 2019-12-04 08:12:31
原文摘自: https://www.cnblogs.com/gwf93/p/10384352.html 先写一个父类Car function Car(name){ this.name = name; this.driver = function (){ console.log('日行千里') } } Car.prototype.addOil = function(param){ this.oil = param; console.log(this.name + '每天加油' + param + 'L') } (一)原型链继承:    核心: 将父类的实例作为子类的原型 instanceOf:测试一个对象是否为一个类的实例 // 原型继承 function BaoMa() { } BaoMa.prototype = new Car() //父类的实例作为子类的原型,继承的关键 BaoMa.prototype.name = "宝马" var baoMa1 = new BaoMa(); // 子类的实例化 console.log(baoMa1.name) console.log(baoMa1.addOil(20)) console.log(baoMa1.driver()) console.log(baoMa1 instanceof Car) // true console.log