JS继承类的写法
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.playGame = function() {
console.log('我能打游戏!!');
}
SmartPhone.prototype.photo = function() {
console.log('我能拍照!!');
}
var onePlus = new SmartPhone('一加', 2499, '黑色', '4.4inch');
console.log(onePlus);
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/07f8ccd633b78cb94c097faa79d8f975.png)
ES6继承类的写法
class Phone{
constructor(brand, price) {
this.brand = brand;
this.price = price;
}
call() {
console.log('打电话!');
}
}
class SmartPhone extends Phone {
constructor(brand, price, color, size){
super(brand, price);
this.color = color;
this.size = size;
}
photo() {
console.log('拍照!!');
}
playGame() {
console.log('打游戏!!');
}
}
const ml = new SmartPhone('小米', 1999, '蓝色', '5.0inch');
console.log(ml);
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4c30a89a10d6bef22da966918e48c4af.png)