Previous: , Up: Interoperability   [Contents]


4.3 Interoperable Polymorphism

GNU ease.js encourages polymorphism through type checking. In the case of prototypal subtyping, type checks will work as expected:

  var Foo = Class( {} );

  function SubFoo() {};
  SubFoo.prototype = Foo.asPrototype();
  SubFoo.constructor = Foo;

  var SubSubFoo = Class.extend( SubFoo, {} );

  // vanilla ECMAScript
  ( new Foo() ) instanceof Foo;           // true
  ( new Subfoo() ) instanceof Foo;        // true
  ( new SubSubFoo() ) instanceof Foo;     // true
  ( new SubSubFoo() ) instanceof SubFoo;  // true

  // GNU ease.js
  Class.isA( Foo, ( new Foo() ) );           // true
  Class.isA( Foo, ( new SubFoo() ) );        // true
  Class.isA( Foo, ( new SubSubFoo() ) );     // true
  Class.isA( SubFoo, ( new SubSubFoo() ) );  // true

Figure 4.4: Type checking with prototypal subtypes of GNU ease.js classes

Plainly—this means that prototypes that perform type checking for polymorphism will accept GNU ease.js classes and vice versa. But this is not the only form of type checking that ease.js supports.

This is the simplest type of polymorphism and is directly compatible with ECMAScript’s prototypal mode. However, GNU ease.js offers other features that are alien to ECMAScript on its own.