Download. The new release has much improved verification of prototypes.
But why use prototypes at all, you may ask, when we now have true classes and inheritance?
Two reasons:
1) You may have legacy code or third-part libraries which relies on prototype inheritance.
2) You may actually like prototypes. Not everyone thinks that classes are the holy grail, and prototype-based inheritance has its own subversive charm.
Whatever the reason, Mascara now supports prototype-based inheritance. There is however some limitations compared to how you can use it in ordinary JavaScript.
You can assign directly to the prototype on a class:
String.prototype.shuffle = function() {...}
However, you cannot assign to a variable expression:
var cls = String;
cls.prototype.shuffle = function(){...} <-- ERROR!
This will generate a compile-time error, since a variable may change, and therefore the type-engine cannot be completely sure what object we are assigning to.
You can however do this:
const cls = String;
cls.prototype.shuffle = function() {...}
Because a 'const' is constant, and therefore can always be resolved by the compiler. Most patterns using prototypes assign directly to the function as in the first example, however some more clever patterns are more dynamic and can sadly not be verified statically.
Another caveat:
Usually we assign to prototypes of constructor functions. But as the example with String shows, we can also assign to the prototype of a class.
However:
1) the class has to be declared as dynamic before we can modify the prototype.
2) You shouldn't do it anyway, since the interaction between class-based inheritance and prototype-based inheritance is undefined. If B extends A, and you modify B.prototype, then you may or may not change the behavior of A, depending on the phases of the moon.
I have added a new sample to the online demo, which shows an example of prototype-based inheritance. Have fun!
søndag den 15. juni 2008
Abonner på:
Kommentarer til indlægget (Atom)
This blog has moved to http://blog.mascaraengine.com. See you!
This blog is (was) for news, announcements and questions regarding the
Mascara ECMAScript 6 -> JavaScript translator.
This blog is (was) for news, announcements and questions regarding the
Mascara ECMAScript 6 -> JavaScript translator.
Ingen kommentarer:
Send en kommentar