//汽车(类),属性:颜色、轮胎、品牌...
//轿车:后备箱
//货车:大货箱
1.原型链的继承
function Person() {
this name = "person",
this arr = [1,2,3,4]
}
console.log(new Person());
function Child() {
this.type = "child"
}
Child prototype = new Person()
var c1 = new Child()
var c2 = new Child()
c1.arr.push(5)
console.log(c1);
console.log(c2);
//子类的实例对象是同一个
//2.构造函数继承
function Person() {
this.name = "Person";
this.arr = [1,2,3,4]
}
Person.prototype.age=18
function Child(){
Person.call(this)
this.type = "child"
}
//console.log(new Child());
var c1 = new Child()
var c2 = new Child()
c1.arr.push(5)
console.log(c1);
console.log(c2);
//构造函数不能继承原型属性的方法和属性
//3.组合继承
function Person() {
this.name = "Person";
this.arr = [1,2,3,4]
}
Person.prototype.age=18
function Child(){
Person.call(this)
this.type = "child"
}
Child.prototype = new Person();
Child.prototype.constructor=Child
var c1 = new Child()
var c2 = new Child()
console.log(c1.constructor);
c1.arr.push(5)
console.log(c1);
console.log(c2);