javascript __proto__ don't produce the same effects that "prototype" inheritance does -
javascript __proto__ don't produce the same effects that "prototype" inheritance does -
the main reason utilize " proto " time trying maintain inheritance definition within function definition:
setup inheritance out side function def, works functions access "public fields" through "this.xxx" , , inheriting_funcdef must have extending knowledge of superfuncdef , other wise "public fields" happen collide:
var g=function (){ var g1state=0; this.g1=function(){ homecoming g1state++; } }; var e = function (){ var e2state=0; this.e2=function(){ homecoming e2state++; } }; e.prototype=new g(); var f= function (){ var f3state=0; this.f3=function(){ homecoming 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 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 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 f3() not inherited super(), , have 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 for "improved version",seems drawback : "instancof" test can not right , otherwise ,it usable. "instancof" incorrectness major drawback.
//i test in ie 11, result same. var g=function (){ var g1state=0; this.g1=function(){ homecoming g1state++; } }; var e = function (){ object.setprototypeof(this,new g()); var e2state=0; this.e2=function(){ homecoming e2state++; } }; //e.prototype=new g(); var f= function (){ object.setprototypeof(this,new e()); var f3state=0; this.f3=function(){ homecoming 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 true console.log("xx instanceof f:___"+(xx instanceof f));//xx instanceof f:___false, expect true console.log("xx instanceof g:___"+(xx instanceof g));//xx instanceof g:___true so either way can't produce perfect result. , think "funcref.prototype=new superfuncref()" way inheritance setup don't work me.
and reason object.setprototypeof(this,new superfuncref()); because wanna "instancof" clause true, otherwise , superfuncref().apply(this), re-create function "this" first,then local override. hence new f() instanceof f, not wanted.
thanks attention. if don't care , or think not worth , please leave alone,don't waste more time down_vote , @ border of , or can teach me english language grammar commenting below . re-format 1 time again , 1 time again till satisfy ,despite give reply or not.
why trying within constructor? that's inefficient , serves no purpose. shouldn't touching __proto__ unless have rare need so.
here orthodox way set inheritance (and not have separate re-create of every fellow member function during execution). note utilize of object.create() rather new:
//test in chrome_v36 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 if want maintain more contained reason, can utilize iife:
//test in chrome_v36 var g = (function() { var g = function() { }; g.prototype.g1 = function() {}; homecoming g; })(); var e = (function () { var e = function() { }; e.prototype = object.create(g.prototype); e.prototype.e2 = function() {}; homecoming e; })(); var f = (function () { var f = function() { }; f.prototype = object.create(e.prototype); f.prototype.f3 = function() {}; homecoming f; })(); however, don't see benefit in doing this. @ to the lowest degree not simplistic example.
javascript inheritance prototype proto
Comments
Post a Comment