bankForJS_inherit

来源:互联网 发布:皇帝岛 珊瑚岛 pp 知乎 编辑:程序博客网 时间:2024/06/05 20:22

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);    };})();//mainvar 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);//zzzconsole.log(person.age);//18console.log(person.work);//doctorconsole.log(person.getName());//zzzconsole.log(person.getAge());//18console.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 parentconsole.log(instance.getChildProperty());//i am child//console.log(instance.hasOwnProperty("parentProperty"));//falseconsole.log(instance.hasOwnProperty("getParentProperty"));//falseconsole.log(instance.hasOwnProperty("childProperty"));//trueconsole.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"));//trueconsole.log("age is "+instance1.age+" in ownProperty:"+instance1.hasOwnProperty("age"));//trueconsole.log("colors is "+instance1.colors+" in ownProperty:"+instance1.hasOwnProperty("colors"));//trueconsole.log("sayName is "+" in ownProperty:"+instance1.hasOwnProperty("sayName"));//flaseconsole.log("sayAge is "+" in ownProperty:"+instance1.hasOwnProperty("sayAge"));//flasevar 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"));//在函数内部重写原型//trueconsole.log("age is "+"in ownProperty:"+person1.hasOwnProperty("age"));//trueconsole.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();

目录

  • privateInherit
    • privateInheritTheory
  • publicInherit
    • publicInheritTheory
    • inheritTheory
    • borrowConstructor
    • prototypeInherit
    • parasiticInherit
    • parasitiCombine
  • heighten
    • singleHeighten
  • 目录