原型链继承
// 定义一个人类
function Person (name) {
// 属性
this.name = name || 'Person';
// 实例方法
this.work = function(){
console.log(this.name +'专业编代码!');
}
}
// 原型方法
Person.prototype.play = function(hover) {
console.log(this.name + '正在:' + hover);
};
functioon teacher(){}
teather.prototype=new person();
teacher.prototype.name='qiaolaoshi'
// Test Code
var lu = new Teacher();
console.log(lu.name);
console.log(lu.play('basketball'));
console.log(lu.work());
console.log(lu instanceof Person); //true
console.log(lu instanceof Teacher); //true
lulaoshi
lulaoshi正在:basketball
lulaoshi专业编代码!
true
true
// 定义一个人类
function Person (name) {
// 属性
this.name = name || 'Person';
// 实例方法
this.work = function(){
console.log(this.name +'专业编代码!');
}
}
// 原型方法
Person.prototype.play = function(hover) {
console.log(this.name + '正在:' + hover);
};
function teacher(){person.call(this);this.name='qiaogege'}
var lu = new Teacher();
console.log(lu.name);
console.log(lu.play('basketball'));
console.log(lu.work());
console.log(lu instanceof Person); //true
console.log(lu instanceof Teacher); //true
// 定义一个人类
function Person (name) {
// 属性
this.name = name || 'Person';
// 实例方法
this.work = function(){
console.log(this.name +'专业编代码!');
}
}
// 原型方法
Person.prototype.play = function(hover) {
console.log(this.name + '正在:' + hover);
};
function teacher(){var ren =new Person();ren.name||''tom';renturn ren}
var lu = new Teacher();
console.log(lu.name);
console.log(lu.play('basketball'));
console.log(lu.work());
console.log(lu instanceof Person);
console.log(lu instanceof Teacher);
// 定义一个人类
function Person (name) {
// 属性
this.name = name || 'Person';
// 实例方法
this.work = function(){
console.log(this.name +'专业编代码!');
}
}
// 原型方法
Person.prototype.play = function(hover) {
console.log(this.name + '正在:' + hover);
};
function Teacher(){
var ren=new Person()
for(var p in ren)/
Teacher.prototype[p]=ren[p]
Teacher.prototype.name=name||"tom"
}
var lu = new Teacher();
console.log(lu.name);
console.log(lu.play('basketball'));
console.log(lu.work());
console.log(lu instanceof Person);
console.log(lu instanceof Teacher);
完美的继承方法//寄生组合继承
// 定义一个人类
function Person (name) {
// 属性
this.name = name || 'Person';
// 实例方法
this.work = function(){
console.log(this.name +'专业编代码!');
}
}
// 原型方法
Person.prototype.play = function(hover) {
console.log(this.name + '正在:' + hover);
};
function teather(){person.call(this) this.name=name||"qiao"}
var f=function(){} f.prototype=person.prototype teacher.protootype=new f() teacher.prototype.constructor=teacher
var lu = new Teacher();
console.log(lu.name);
console.log(lu.work());
console.log(lu instanceof Person);
console.log(lu instanceof Teacher);
// 定义一个人类
function Person (name) {
// 属性
this.name = name || 'Person';
// 实例方法
this.work = function(){
console.log(this.name +'专业编代码!');
}
}
// 原型方法
Person.prototype.play = function(hover) {
console.log(this.name + '正在:' + hover);
};
function Teacher(){
Person.call(this)//过继了父类的属性
this.name=name||"qiao"
}
function extend(Teacher,Person){
var f=function(){}
f.prototype=Person.prototype
Teacher.prototype=new f()//继承了父类的原型//且只实 例化了一次父类
Teacher.prototype.constructor=Teacher
}
extend(Teacher,Person)
var lu = new Teacher();
console.log(lu.name);
console.log(lu.work());
console.log(lu instanceof Person);
console.log(lu instanceof Teacher);
子类继承父类,父类有两种方法,重写其中一种方法
// supcalss
var parent = function(name,age){
this.name = name;
this.age = age;
}
parent.prototype.showProper = function()
{
console.log(this.name+":"+this.age);
}
var child = function(name,age){
parent.call(this.name,age);
}
// child.prototype = new parent();
// 继承 创建一个以proto对象为原型对象的对象,并且返回这个对象
child.prototype = Object.create(parent.prototype);
//修复子类构造函数的指向
child.prototype.constructor = child;
// 重写构造函数 两种方式 第一种真正重写,第二种,只是添加属性
//child.prototype.__proto__.showProper = function(){}
child.prototype.showProper = function(){
console.log('I am '+this.name+":"+this.age);
}
var obj =new child('wozien','22');
obj.showProper();