bankForJS_inherit

本文深入探讨JavaScript中的多种继承模式,包括私有继承、公共继承、借用构造函数、原型继承及寄生组合继承等,并对比了不同模式的特点及应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值