//定义一个父类
function Father(name) {
this.name = name;
this.age = 20;
}
Father.prototype.say = function () {
console.log(this.name);
}
//定义一个子类
function Child(name,age) {
this.name = name;
this.age = age
}
//最重要的一步,将父类的实例为子类的原型
Child.prototype = new Father();
let child1 = new Child('Marry',10);
let child2 = new Child('luccy',17);
缺点:
//定义一个父类
function Father(name) {
this.name = name;
this.age = 20;
}
Father.prototype.say = function () {
console.log(this.name);
}
//定义一个子类
function Child(name,age) {
Father.call(this,'father');
this.name = name;
this.age = age
}
let child1 = new Child('Marry',10);
let child2 = new Child('luccy',17);
缺点:
优点:
//定义一个父类
function Father(name) {
this.name = name;
this.age = 20;
}
Father.prototype.say = function () {
console.log(this.name);
}
//定义一个子类
function Child(name,age) {
Father.call(this,'father');
this.name = name;
this.age = age
}
Child.prototype = new Father();
let child1 = new Child('Marry',10);
let child2 = new Child('luccy',17);
特点:
//定义一个父类
function Father(name) {
this.name = name;
this.age = 20;
}
Father.prototype.say = function () {
console.log(this.name);
}
//这里是核心步骤
function content(obj) {
function F() {};
F.prototype = obj;
return new F();
}
var sup = content(new Father())
缺点:
//定义一个父类
function Father(name) {
this.name = name;
this.age = 20;
}
Father.prototype.say = function () {
console.log(this.name);
}
function content(obj) {
function F() {};
F.prototype = obj;
return new F();
}
function a() {
var sup = content(new Father());
sup.name = 'Tom'
return sup;
}
var child1 = a();
缺点: 没有用到原型,无法复用
函数的原型等于另一个实例
在函数中用apply或者call引入另一个构造函数
//定义一个父类
function Father(name) {
this.name = name;
this.age = 20;
}
Father.prototype.say = function () {
console.log(this.name);
}
function content(obj) {
function F() {};
F.prototype = obj;
return new F();
}
function Child(name) {
Father.call(this,'father');
this.name = name;
}
var sup = new Father();
Child.prototype = content(sup);
sup.constructor = Child;
var child1 = new Child('Marry')
博客原文https://www.cnblogs.com/ranyonsue/p/11201730.html
原文:https://www.cnblogs.com/fanzhikang/p/12497243.html