javascript __proto__不会产生与“原型”继承相同的效果

时间:2014-10-14 07:31:38

标签: javascript inheritance prototype proto

使用"的主要原因 proto "这次试图将继承定义保留在函数定义中:

设置继承外侧功能def,仅适用于仅访问"公共字段"通过" this.xxx" ,且Inheriting_FuncDef必须具有SuperFuncDef的扩展知识,其他方面必须具有"公共字段"碰巧碰撞:

var G=function (){
    var g1state=0;
    this.g1=function(){
        return g1state++;
    }
};
var E = function (){

    var e2state=0;
    this.e2=function(){
        return e2state++;
    }
};
E.prototype=new G();

var F= function (){

    var f3state=0;
    this.f3=function(){
        return f3state++;
    }
};
F.prototype=new E();


var xx = new F();
var xx2= new F();

console.log("xxg1:___"+xx.g1());//0
console.log("xxg1:___"+xx.g1());//1
console.log("xx2g1:___"+xx2.g1());//2 , need it to be 0, don't wanna share same super() instance/and closure.


console.log("xxe2:___"+xx.e2());//0
console.log("xxe2:___"+xx.e2());//1
console.log("xx2e2:___"+xx2.e2());//2 , need it to be 0;don't wanna share same super() instance/and closure.


console.log("xxf3:___"+xx.f3());//0
console.log("xxf3:___"+xx.f3());//1
console.log("xx2f3:___"+xx2.f3());//0 this f3() is not inherited from super(), and have the expected result. 

console.log(xx);
console.log("xx instanceof E:___"+(xx instanceof E));//ture
console.log("xx instanceof F:___"+(xx instanceof F));//true
console.log("xx instanceof G:___"+(xx instanceof G));//ture

对于"改进版本",似乎唯一的缺点是:" instancof"测试不能正确,否则,它是可用的。但是" instancof"不正确是一个主要缺点。

//i test it in ie 11, the result is the same.
var G=function (){
    var g1state=0;
    this.g1=function(){
        return g1state++;
    }
};
var E = function (){
    Object.setPrototypeOf(this,new G());
    var e2state=0;
    this.e2=function(){
        return e2state++;
    }
};
//E.prototype=new G();
var F= function (){
    Object.setPrototypeOf(this,new E());
    var f3state=0;
    this.f3=function(){
        return f3state++;
    }
};
//F.prototype=new E();

var xx = new F();
var xx2= new F();

console.log("xxg1:___"+xx.g1());//xxg1:___0  ,expected.
console.log("xxg1:___"+xx.g1());//xxg1:___1  ,expected.
console.log("xx2g1:___"+xx2.g1());//xx2g1:___0  ,expected.


console.log("xxe2:___"+xx.e2());//xxe2:___0  ,expected.
console.log("xxe2:___"+xx.e2());//xxe2:___1  ,expected.
console.log("xx2e2:___"+xx2.e2());//xx2e2:___0  ,expected.


console.log("xxf3:___"+xx.f3());//xxf3:___0  ,expected.
console.log("xxf3:___"+xx.f3());//xxf3:___1  ,expected.
console.log("xx2f3:___"+xx2.f3());//xx2f3:___0  ,expected.


console.log(xx);
console.log("xx instanceof E:___"+(xx instanceof E));//xx instanceof E:___false , expect to be true
console.log("xx instanceof F:___"+(xx instanceof F));//xx instanceof F:___false, expect to be true
console.log("xx instanceof G:___"+(xx instanceof G));//xx instanceof G:___true

所以无论哪种方式都无法产生完美的结果。我认为" Funcref.prototype = new superFuncref()"继承设置的方式基本上不适合我。

和我做Object.setPrototypeOf的唯一原因(这个,新的SuperFuncRef());是因为我想要所有" instancof"子句为真,否则,我会做SuperFuncRef()。apply(this),将所有函数复制到"这个"首先,然后执行本地覆盖。因此新的F()只是F的一个实例,这不是我想要的。

谢谢你的关注。如果你不关心它,或者认为不值得,请不要管它,不要浪费更多时间来挫败它,我处于边缘,或者你可以通过下面的评论教我英语语法它。我会一次又一次地重新格式化,直到你满意为止,尽管你给出了答案。

3 个答案:

答案 0 :(得分:2)

为什么要尝试在构造函数中执行所有操作?这是低效的,没有任何目的。除非您有一些罕见的需要,否则您也不应该触及__proto__

这是一种设置继承的正统方法(并且在执行期间没有每个成员函数的单独副本)。请注意使用Object.create()而不是new

//test in chrome_v36 only
var G = function() {
};
G.prototype.g1 = function() {};

var E = function() {
};
E.prototype = Object.create(G.prototype); 
E.prototype.e2 = function() {};

var F = function() {
};
F.prototype = Object.create(E.prototype); 
F.prototype.f3 = function() {};

var xx = new F();
console.log(xx); //F {f3: function, e2: function, g1: function}
console.log("xx instanceof E:___" + (xx instanceof E)); // true
console.log("xx instanceof F:___" + (xx instanceof F)); // true
console.log("xx instanceof G:___" + (xx instanceof G)); // true

如果由于某种原因想要保留更多内容,可以使用IIFE:

//test in chrome_v36 only
var G = (function() {
   var g = function() {
   };

   g.prototype.g1 = function() {};

   return g;
})();

var E = (function () {
    var e = function() {
    };

    e.prototype = Object.create(G.prototype); 
    e.prototype.e2 = function() {};

    return e;
})();

var F = (function () {
    var f = function() {
    };

    f.prototype = Object.create(E.prototype); 
    f.prototype.f3 = function() {};

    return f;
})();

但是,我真的认为没有任何好处。至少不是这个简单的例子。

答案 1 :(得分:0)

包含原型分配滥用的自包含隔离继承?可以做到。

function Class1 () {
   if (!Class1.done) {
      Class1.prototype.meth = function(){}
      Class1.done = true
   }
   this.ownBaseField = 1
}

function Class2 () {
   Class1.call(this)
   if (!Class2.done) {
      Class2.prototype.__proto__ = Class1.prototype
      Class2.done = true
   }
}

function Class3 () {
   Class2.call(this)
   if (!Class3.done) {
      Class3.prototype.__proto__ = Class2.prototype
      Class3.done = true
   }
}

var o = new Class3()
;[o instanceof Class3, o instanceof Class2, o instanceof Class1, 'meth' in o]
// true,true,true,true

将此示例仅视为练习 - 强烈建议不要这样做(原型分配)。

答案 2 :(得分:0)

答案是在这个链接中提供的,虽然没有具体回答这个,但机制是相同的:

https://stackoverflow.com/a/26300006/289701

以前我不知道的那个艰难而悲伤的事实是: 构造函数的每个实例都有相同的构造函数,因此构造函数也是相同的。对于FuncDef的每个实例,只有一个“instance._proto__”不能被伪造:

function ClassNameHere (){}
var x = new ClassNameHere ();
var x2 = new ClassNameHere ();
console.log(x.__proto__===ClassNameHere.prototype)// true.
console.log(x.__proto__===x2.__proto__)// true.

所以没有办法只使用“javascript继承”来构建java_Class相似的功能:(private_fields,inheritance)

我之前不知道的第二个难以理解的事实是,“javascrpit继承”只需要设置一次。

解决方案是使用SuperFuncRef.call(this)复制SuperFunc的所有方法并为这些复制的方法创建新的范围,然后执行impl覆盖。同时,使用“light_weight的SuperFunc实例”来“伪造”/表达“inheritance_relationship / chain”

我不知道它是否是邪恶的代码,但结果是我想要的:

var E= function(c){if(c) return this;

    var ex= 0;
    this.x=function(){
        return ex++;
    };
}

var F= function(c){

    if(!(this instanceof E)){
        this.__proto__.__proto__=new E(true);
    }
    if(c) return this;
    E.call(this);

    var fx=0;
    this.y=function(){
        return fx++;
    };
}

var G= function(c){

    if(!(this instanceof F)){
        this.__proto__.__proto__=new F(true);
    }
    if(c) return this;
    F.call(this);

    var lsuper={};


    var gx=0;

    this.z=function(){
        return gx++;
    };

    if(this.y)lsuper.y=this.y;
    this.y=function(){
        return lsuper.y()+"hehe";
    }
}

var x=new G();
console.log("-------------")
var x2= new G();
console.log("-------------")
var x3= new G();
console.log("x.x():___"+x.x());//0
console.log("x.x():___"+x.x());//1
console.log("x.x():___"+x.x());//2
console.log("x2.x():___"+x2.x());//0, expected, different scope
console.log("x2.y():___"+x2.y());//0hehe

console.log(x);

console.log("x instanceof G:___"+(x instanceof G));//true
console.log("x instanceof F:___"+(x instanceof F));//true
console.log("x instanceof E:___"+(x instanceof E));//true

console.log("x2 instanceof G:___"+(x2 instanceof G));//true
console.log("x2 instanceof F:___"+(x2 instanceof F));//true
console.log("x2 instanceof E:___"+(x2 instanceof E));//true

console.log("x3 instanceof G:___"+(x3 instanceof G));//true
console.log("x3 instanceof F:___"+(x3 instanceof F));//true
console.log("x3 instanceof E:___"+(x3 instanceof E));//true