# 继承

'use strict'
function Car(brand, color, price) {
    this.brand = brand;
    this.color = color;
    this.price = price
}
Car.type = "车"

Car.prototype.sell = function() {
    console.log("我是"+ this.color + "色的" + this.brand + ", 我的售价是" + this.price);
}

function BMW(brand, color, price) {
    Car.call(this, brand, color, price);
}

//子类继承父类静态属性
var staticKeys = Object.entries(Car);
for(var i = 0; i < staticKeys.length; i++) {
    var key = staticKeys[i][0];
    var value = staticKeys[i][1];
    BMW[key] = value;
}
console.log(BMW.type);//车

// var __proto = Object.create(Car.prototype);
// __proto.constructor = BMW;
// BMW.prototype = __proto;
BMW.prototype = Object.create(Car.prototype, {
    constructor:{
        value: BMW,
        writable:false
    }
});
// BMW.prototype = 123
var bmw = new BMW("宝马", "红", "30万");
bmw.sell();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
最后更新时间: 9/26/2020, 7:38:09 PM