Loading ...
Sorry, an error occurred while loading the content.

913Inheritance issue

Expand Messages
  • greg.hinch
    Sep 25, 2009
    • 0 Attachment
      I'm having some issues creating Widgets and using instanceof, and I think the best way I can illustrate it is with an example. I'll preface by saying I know the Widget class is still under development, but I'd like to know if this pattern I'm trying to develop will work but there's just a bug right now, or rather if I'm going about my development the wrong way. With that in mind, I've created this little sample set of classes that illustrate the problem I'm having.

      I'm creating a "farm" component to contain Animals. The Farm component has been build as several classes, Farm, Animal, FourLegs, and Cat, and built using the YUI Builder to create a single script file, and all classes are then added to the Y namespace. Y.Cat extends Y.FourLegs, which extends Y.Animal, which extends Y.Widget. Y.Farm extends Y.Widget. An animal instance, when rendered, will spit out the name of its top-most class, for example Y.Cat will display "A cat has been born". A farm is instantiated with a property "animals", which is an array of instances of the Y.Animal class. When rendered, it will display which animals are in the farm, and how many failed to pass the validation. Animals are validated by passing an (X instanceof Y.Animal) check. If they fail, they are removed from the array and the failed count increases by 1.

      My implementation consists of an inline script, wrapped in YUI.use and loading the "farm" component, which declares a new animal type, Dog, instantiates one of each animal type and a farm, renders the animals, and then the farm. Also on the page I've loaded an external script to declare another animal type, a Horse. In that script I've called YUI.use, again loading "farm", and created the animal in the same way. I then placed that constructor in the my.animals.horse global namespace.

      The problem I'm having is that any instance of Horse fails to pass the instanceof Y.Animal check in the farm. If I dump out the object that's created via Firebug, it appears to be exactly the same type of object as Dog and Cat, and it was certainly built the same way, but instanceof still fails. Here's the page showing what I just described:

      http://www.greghinch.com/yui3test/farm.html

      Am I just going about this in the wrong way or is this just a bug in Widget that will be resolved by the time it goes GA?
      Thanks!

      Greg
    • Show all 5 messages in this topic