构造函数继承
function Phone(brand, price) {
this.brand = brand
this.price = price
}
Phone.prototype.call = (function() {
console.log(‘打电话请给我’)
})
function SmartPhone(brand, price, color, size) {
Phone.call(this, brand, price)
this.color = color
this.size = size
}
SmartPhone.prototype = new Phone
SmartPhone.prototype.constructor = SmartPhone
SmartPhone.prototype.photo = function() {
console.log('拍照')
}
SmartPhone.prototype.playGame = function() {
console.log('玩游戏')
}
const chuizi = new SmartPhone('锤子', 3333, '红色', '3.3yincun')
console.log(chuizi)

每日一句
在泪水中浸泡过的微笑最灿烂,从迷惘中走出来的灵魂最清醒。

本文介绍了使用构造函数实现继承的方法,并通过一个具体的例子展示了如何让一个子类继承父类的属性和方法,同时保留自身独有的特性。
916

被折叠的 条评论
为什么被折叠?



