Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Parasitic Inheritance

I'm struggling with Javascript classical Inheritance 1. While, in the end Douglas Crockford, rejects its first attemps to support classical model in Javascript, I find it interesting to understand:

I have been writing JavaScript for 8 years now, and I have never once found need to use an uber function. The super idea is fairly important in the classical pattern, but it appears to be unnecessary in the prototypal and functional patterns. I now see my early attempts to support the classical model in JavaScript as a mistake.

However, there is something not really clear for what concerns Parasitic Inheritance:

function ZParenizor2(value) {
   var that = new Parenizor(value);
   that.toString = function () {
      if (this.getValue()) {
         return this.uber('toString');
      }
      return "-0-"
   };
   return that;
 }

ZParenizor, as far I've understood is defined in terms of Parenizor. But Parenizor is the base class and it inherits from nobody. So this.uber function will not be defined and in fact I've an error when I call the toString method of a new ZParenizor2 object.

Am I correct, or I'm ignoring something?

UPDATE I was right. This method only works when you create ZParenizor with 0 as parameter, since it does not need to call the uber method (as you can see form the method imeplementation).

When you try it with a different parameter, I get this error:

Uncaught TypeError: Object #<error> has no method 'uber'
like image 987
user278064 Avatar asked Aug 30 '11 22:08

user278064


1 Answers

You can modify Parenizor's prototype to have a uber function and it will work with any other value.

like image 95
Chiguireitor Avatar answered Oct 06 '22 00:10

Chiguireitor