//原型方法
function ClassA(){
}
ClassA.prototype = {
color:'red',
sayColor: function (){
alert(this.color);
}
}
function ClassB(){
this.name = "";
this.sayName = function(){
alert(this.name);
}
}
ClassB.prototype = new ClassA();
var objA = new ClassA();
var objB = new ClassB();
objA.color = 'red';
objB.color = 'blue';
objB.name = 'caibinghong';
objA.sayColor();
objB.sayColor();// b类通过原型的方法 继承 A 类
objB.sayName();
//对象冒充 方法实继承
function person(){
this.name = 'person';
this.getName = function(){
alert(this.name);
}
}
function worker(name,age){
//对象冒充继承
this.constructor = person;
this.constructor(name);
delete this.constructor;
this.name = name||'caibinghong';
this.age = age||'2012';
this.getAge = function(){
alert(this.age);
}
}
var man = new person();
var xiaofan = new worker('xiaofan',28);
man.getName();
xiaofan.getName();
xiaofan.getAge();
// call 方法实继承
var ClassC = function(sColor,tt){
this.color = sColor;
this.tt = tt;
this.sayColor = function (){
alert(this.color+this.tt);
}
}
var ClassD = function(sColor,sName){
ClassC.call(this,sColor,sName);//call 方法继承 传参 只能 以逗号隔开
this.name = sName;
this.sayName = function(){
alert(this.name);
}
}
var objC = new ClassC('___red','_cc : call 方法 传参 只能 以逗号隔开');
var objD = new ClassD('___blue','___D_caibinghong : call 方法 传参 只能 以逗号隔开');
objC.sayColor();
objD.sayColor();
objD.sayName();
// apply 方法实继承
var ClassE = function(sColor,tt){
this.color = sColor;
this.tt = tt;
this.sayColor = function (){
alert(this.color+this.tt);
}
}
var ClassF = function(sColor,sName){
ClassE.apply(this,[sColor,sName]);//apply 方法继承 传参 只能数组
this.name = sName;
this.sayName = function(){
alert(this.name);
}
}
var objE = new ClassC('___red','____ee : apply 方法 传参 只能数组');
var objF = new ClassD('___blue','___F_caibinghong : apply 方法 传参 只能数组');
objE.sayColor();
objF.sayColor();
objF.sayName();
//根据上面的几种方法,下面来整合一下.
var fdauto = function (age){
this.age = age;
}
fdauto.prototype = {
getAge:function (){
alert(this.age);
}
}
var fdAm = function (age,name){
fdauto.call(this,age);//构造??
this.name = name;
}
fdAm.prototype = new fdauto();//继承?? 这里有点不是很明白
fdAm.prototype.getName=function (){
alert(this.name);
};
var _fdauto = new fdauto(12);
var _fdAm = new fdAm(10,'the am of fdauto!!');
_fdauto.getAge();
_fdAm.getAge();
_fdAm.getName();