function Person(color){
this.color = color;
}
第一种 原型链继承(无法为父类传值)
Student.prototype = new Person("");
Student.prototype.conStructor = Student;
function Student(name,age,color){
this.name = name;
this.age = age;
this.hello = function(){
console.log(this.color);
}
}
第二种调用构造函数(无法继承父类原型方法)
function Student(name,age,color){
Person.call(this,color)
this.name = name;
this.age = age;
this.hello = function(){
console.log(this.color);
}
}
第三种 组合继承(两次实例化父类 继承属性 缺点:消耗内存 子类屏蔽一次父类实例化对象)
function Student(name,age,color){
Person.call(this,color)
this.name = name;
this.age = age;
}
Person.prototype.hello = function(){
console.log(this.color);
}
Student.prototype = new Person("");
Student.prototype.conStructor = Student;
// 第四种 使用一个空对象去创建一个父类原型 减小内存开销.
Person.prototype.hello = function(){
console.log(this.color);
}
function Student(name,age,color){
Person.call(this,color)
this.name = name;
this.age = age;
}
(function(){
var tmp = function(){}
tmp.prototype = Person.prototype;
Student.prototype = new tmp();
Student.prototype.constructor = Student;
})()
const s1 = new Student('zhangsan',20,'绿色');
console.dir(s1.proto)
console.dir(s1)
s1.hello();
(function(num){
console.log(num)
})(19)