开发者

Javascript Parasitic Inheritance

开发者 https://www.devze.com 2023-03-31 16:59 出处:网络
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'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 pro开发者_开发技巧totypal 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'


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

0

精彩评论

暂无评论...
验证码 换一张
取 消