Why won't my efforts to implement parasitic inheritance in Javascript work here?

I play with classic inheritance in Javascript (in Chrome), following the example of Crockford . I like the parasitic interface, but I'm looking for a cleaner way to encapsulate inheritance.

There are a few additional requirements that I'm trying to fulfill:

  • I would like to override the methods of the parent class using methods in the child class that can call the parent method
  • I do not want to re-declare the properties of the parent class in the child class.

This is my Parasite parent class using the extend method, which attempts to encapsulate inheritance and use Object.defineProperty :

 function Parasite(host) { var self = {}; self.host = host; self.swollen = false; self.init = function() { console.debug('Parasite.init'); return self; }; self.suck = function() { console.log("I'm a parasite who sucks on " + self.host); self.swollen = true; return self; }; self.extend = function(child) { for(var prop in self) { if (prop == 'extend') { // skip extend console.debug('skip extend'); continue; } var is_extended = child.hasOwnProperty(prop); var is_accessor = typeof self[prop] != "function"; // inherit prop if (! is_extended) { child[prop] = self[prop]; console.debug('prop', prop, 'inherited by child'); } // default: override else { console.debug('prop', prop, 'overridden by child'); } // For accessors, parent should reference child. This tries to // synchronize them on the child accesor. if (is_accessor) { var accessor = prop.toString(); console.warn('define accessor for', accessor, ':', child[accessor]); Object.defineProperty(self, accessor, { get: function() { var val = child[accessor]; console.debug('getting', accessor, val, 'from', child, 'for', self); return val; }, set: function(val) { console.debug('setting', accessor, val, 'from', child, 'for', self); child[accessor] = val; }, enumerable: true, configurable: true }); }; } child.parent = self; return child; }; self = self.init(); return self; } 

This is my child class, Tick :

 function Tick(host) { var self = {}; self.suck = function() { self.parent.suck.call(); self.engorge(); }; self.engorge = function() { console.log("And now I'm engorged with blood."); }; self.init = function() { var parent = new Parasite(host); self = parent.extend(self); return self; }; self = self.init(); return self; } 

Here you can find my last fiddle (warning: repeats endlessly in Firefox, but not in Chrome):

http://jsfiddle.net/SSDgv/23/

Console output shows where access problems occur.

+4
source share
1 answer

This is not an answer to my question, in fact, but here is a much simpler implementation that seems to fit my requirements. It follows the pattern provided in this stack overflow answer .

 function Parasite(host) { var self = {}; self.host = host; self.hungry = true; self.init = function() { console.debug('Parasite.init'); return self; }; self.suck = function() { console.log("I'm a parasite who sucks on " + self.host); self.hungry = false; return self; }; self.init(); return self; } function Tick(host) { var self = new Parasite(host); self.engorged = false; var base_suck = self.suck; self.suck = function() { base_suck(); self.engorge(); }; self.engorge = function() { console.log("And now I'm engorged with blood."); self.engorged = true; }; self.init = function() {}; self.init(); return self; } 

The test script can be found here: http://jsfiddle.net/AvdK2/3/

+1
source

Source: https://habr.com/ru/post/1501970/


All Articles