bankForJS_inherit
#privateInherit
##privateInheritTheory
//Parent
(function(){
//private
var a=10;
var b=20;
//public
Parent=function(){};
Parent.prototype.getA=function(){
console.log(a);
};
Parent.prototype.getB=function(){
console.log(b);
};
})();
//Child
(function(){
//private
var c=30;
var d=40;
//public
Child=function(){};
Child.prototype=new Parent();
Child.prototype.getC=function(){
console.log(c);
};
Child.prototype.getD=function(){
console.log(d);
};
})();
//main
var ins=new Child();
ins.getA();
ins.getB();
ins.getC();
ins.getD();
comprehendByC++
class Child{
private:
static var a;
static var b;
var c;
var d;
public:
static getA;
static getB;
static getC;
static getD;
};
#publicInherit
##publicInheritTheory
function Parent(){
this.name="zzz";
this.age=18;
}
Parent.prototype.getName=function(){return this.name;};
Parent.prototype.getAge=function(){return this.age;};
//
function Child(){
this.work="doctor";
}
Child.prototype=new Parent();
Child.prototype.constructor=Child;
Child.prototype.getWork=function(){return this.work;};
var person=new Child();
console.log(person.name);//zzz
console.log(person.age);//18
console.log(person.work);//doctor
console.log(person.getName());//zzz
console.log(person.getAge());//18
console.log(person.getWork());//doctor
comprehendByC++
class Child{
public:
static this.name="zzz";
static this.age=18;
static getName;
static getAge;
static getWork;
var work="doctor";
}
##inheritTheory
function Parent(){
this.parentProperty="i am parent";
}
Parent.prototype.getParentProperty=function(){return this.parentProperty;};
//
function Child(){
this.childProperty="i am child";
}
Child.prototype=new Parent();
Child.prototype.getChildProperty=function(){return this.childProperty;};
//Child.prototype.constructor=Child;
var instance=new Child();
console.log(instance.getParentProperty());//i am parent
console.log(instance.getChildProperty());//i am child
//
console.log(instance.hasOwnProperty("parentProperty"));//false
console.log(instance.hasOwnProperty("getParentProperty"));//false
console.log(instance.hasOwnProperty("childProperty"));//true
console.log(instance.hasOwnProperty("getChildProperty"));//false
##borrowConstructor
function Parent(name){
this.name=name;
this.colors=["r","g","b"];
}
Parent.prototype.sayName=function(){alert(this.name);};
//
function Child(name,age){
Parent.call(this,name);//相当于调用了内联函数Parent
this.age=age;
}
Child.prototype=new Parent();
Child.prototype.sayAge=function(){alert(this.age);};
//
var instance1=new Child("zzz",18);
console.log("name is "+instance1.name+" in ownProperty:"+instance1.hasOwnProperty("name"));//true
console.log("age is "+instance1.age+" in ownProperty:"+instance1.hasOwnProperty("age"));//true
console.log("colors is "+instance1.colors+" in ownProperty:"+instance1.hasOwnProperty("colors"));//true
console.log("sayName is "+" in ownProperty:"+instance1.hasOwnProperty("sayName"));//flase
console.log("sayAge is "+" in ownProperty:"+instance1.hasOwnProperty("sayAge"));//flase
var instance2=new Child("bbb",26);
console.log(instance1.colors==instance2.colors);//false
##prototypeInherit
function createInsForChild(insForParentObj){
function Child(){}
Child.prototype=insForParentObj;
return new Child();
}
var person={
name:"zzz",
age:18,
friends:["A","B","C"]
};
//格式类似Object.defineProperties()
var person1=Object.create(person,{
name:{value:"bbb"}
});
person1.age=28;
person1.friends.push("D");
console.log("name is "+"in ownProperty:"+person1.hasOwnProperty("name"));//在函数内部重写原型//true
console.log("age is "+"in ownProperty:"+person1.hasOwnProperty("age"));//true
console.log("friends is "+"in ownProperty:"+person1.hasOwnProperty("friends"));//false
##parasiticInherit
function parasiticInherit(insForParentObj){
var clone=Object.create(insForParentObj);
clone.sayHi=function(){alert(hi);};
return clone;
}
##parasitiCombine
- the best
function parasiticCombine(functionForParent,functionForChild){
var prototype=Object.create(functionForParent.prototype);
prototype.constructor=functionForChild;
functionForChild.prototype=prototype;
}
#heighten
##singleHeighten
instance=function heighten(){//函数名可省略
var o=new Object();
var a=10;
var b=20;
o.getA=function(){console.log(a)};
o.getB=function(){console.log(b)};
return o;
}();
//instance为global,instance.getA=o.getA,getA引用了a
//就是global的instance的fun引用了a,所以heighten不会消亡
instance.getA();
instance.getB();