Claude Pache claude.pache at gmail.com
Fri Jun 28 19:41:18 PDT 2013



Le 29 juin 2013 à 00:14, Eric Elliott <eric at ericleads.com> a écrit :

> <snip>
> I saw a recent example of one of the problems with single-ancestor inheritance in a talk. I wish I could remember which one. The illustrations were great:
> 
> Animal
>   - Walking
>     - Monkey
>     - Human
>   - Flying
>     - Bird
>     - Bee
>   - Swimming
>     - Fish
>     - Whale
> 
> Now we need alligator and duck.

Interesting challenge! It may be hard to resolve in some languages, but in JavaScript, you merely need some imagination. Given:

```js
class Flying extends Animal {
    // implementation of Flying
}

class Swimming extends Animal {
    // implementation of Swimming
}
```

it could be quickly refactored as following (I assume that the implementation of Swimming is sufficiently well-written; otherwise some amendments in its implementation might be needed):

```js
class Flying extends Animal {
    // implementation of Flying
}

function addSwimming(Animal) {
    class Swimming extends Animal {
        // implementation of Swimming
    }
    return Swimming
}

var Swimming = addSwimming(Animal)

var FlyingSwimming = addSwimming(Flying)

class Duck extends FlyingSwiming { }
```

(It is true that `Duck instanceof Swimming` doesn't hold; however, duck-typing forbids the use of `instanceof` and even any typelike-check.)

Note how JavaScript remains dynamic, even in the face of the ES6-class-sugar.


—Claude
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130629/e177d8cb/attachment.html>


More information about the es-discuss mailing list