Easy conversion of mixed types

Given the ability to convert between types at will, the rules describe which conversions will occur when there is a mixture of types present. Step one is to identify what is a legal mixture and what isn't. Here it is according to the ECMAScript standard.

A mixture of types can only occur for binary operators. If a given binary operator only supports one type, then both pieces of data for that operator must either be of that type, or convertible to that type, otherwise an error results.

This takes care of most cases, such as all the bit operations, multiplication, subtraction and division. All those operators require two numeric (or convertible to numeric) types.

Remove these cases and the polymorphic binary operators are left—those that work with more than one type of data. The assignment operators and the comma operator are examples, but they are straightforward. The assignment operators always change the contents of their left-hand data item to match their right-hand item, and the comma operator doesn't combine its two data types in any meaningful way at all.

For all these cases, there's no confusion possible, and no need to do anything, except watch for complaints from the JavaScript interpreter.

© 1997 by Wrox Press. All rights reserved.