oop - Is there anything wrong with this approach to calling super() in javascript? -
i'm working on project , decided best way implement functionality wanted override method. @ time didn't realize javascript had no notion of calling super() started doing research.
i found article (http://blog.salsify.com/engineering/super-methods-in-javascript) describes several methods of calling super methods.
i wan't super happy ofthose options though , came following. available on fiddle @ https://jsfiddle.net/fpgm8j9n/.
var food = function( name ){ this.name = name; } food.prototype.sayname = function(){ console.log( 'i ' + this.name ); } var fruit = function( name, color ){ food.call( this, name ); this.color = color; this.super = object.getprototypeof( object.getprototypeof( ) ); } fruit.prototype = object.create( food.prototype ); fruit.prototype.sayname = function(){ console.log( 'i fruit , color ' + this.color ); } var orange = new fruit( 'apple', 'red' ); // runs overridden method in orange orange.sayname(); // fruit , color red // runs super method orange.super.sayname.call( orange ); // apple
the following first example in article posted. these same without having know parent prototype? there thing wrong implementation came or can improved? i'm pretty new oop in javascript , feel kind of wobbly lot of concepts.
var child = parent.extend({ // ... dosomething: function(x, y) { this.dosomethingelse(x); return parent.prototype.dosomething.call(this, x, y); } });
the common use case super
overriding method call method overrode (thereby using existing functionality , further extending more code). in example:
fruit.prototype.sayname = function(){ this.super.sayname.call(this); // prints "i apple" console.log( 'i color ' + this.color ); // prints "i color red" } var orange = new fruit( 'apple', 'red' ); orange.sayname();
calling superclass method of object outside methods (as in orange.super.sayname.call( orange );
arguably non-oo practice. user of object should not need know type or supertype are. should able ask (like print information itself) , object should figure out how on own.
the super
field created works purpose, in allows overriding methods call methods override. however, break down if inheritance hierarchy deeper 1 level:
var grape = function(variety) { fruit.call(this, "grape", "purple"); this.variety = variety; }; grape.prototype = object.create(fruit.prototype); grape.prototype.sayname = function() { this.super.sayname.call(this); console.log('i ' + this.variety + ' grape'); }; var concordgrape = new grape("concord"); concordgrape.sayname(); // unbounded recursion / causes stack overflow
the reason this.super
field remains same regardless of level of hierarchy uses it:
this // grape object object.getprototypeof(this) // grape.prototype object.getprototypeof(object.getprototypeof(this)) // fruit.prototype
so when grape.prototype.sayname
calls this.super.sayname
, it's calling fruit.prototype.sayname
, intended. when fruit.prototype.sayname
calls this.super.sayname
, unfortunately calling itself.
this cannot fixed redefining super
@ each level:
var grape = function(variety) { fruit.call(this, "grape", "purple"); this.variety = variety; this.super = object.getprototypeof( object.getprototypeof( ) ); };
this
points same object regardless of function in hierarchy referring it.
what needed super
know hierarchy level of function using (so invoke corresponding function level above). i'm not aware of foolproof way this, other in article linked.
Comments
Post a Comment