Udostępnij za pośrednictwem


Coercion in JScript

JScript can perform operations on values of different types without the compiler raising an exception. Instead, the JScript compiler automatically changes (coerces) one of the data types to that of the other before performing the operation. Other languages have much stricter rules governing coercion.

Coercion Details

The compiler allows all coercions unless it can prove that the coercion will always fail. Any coercion that may fail generates a warning at compile time, and many produce a run-time error if the coercion fails. For example:

Operation

Result

Add a number and a string

The number is coerced into a string.

Add a Boolean and a string

The Boolean is coerced into a string.

Add a number and a Boolean

The Boolean is coerced into a number.

Consider the following example.

var x = 2000;      // A number.
var y = "Hello";   // A string.
x = x + y;         // the number is coerced into a string.
print(x);          // Outputs 2000Hello.

To explicitly convert a string to an integer, you can use the parseInt Method. For more information, see the parseInt Method. To explicitly convert a string to a number, you can use the parseFloat method. For more information, see the parseFloat Method. Notice that strings are automatically converted to equivalent numbers for comparison purposes but are left as strings for addition (concatenation).

Since JScript is also a strongly typed language, another coercion mechanism is available. The new mechanism uses the target type name as if it were a function that accepts the expression to convert as an argument. This works for all JScript primitive types, JScript reference types, and .NET Framework types.

For example, the following code converts an integer value to a Boolean:

var i : int = 23;
var b : Boolean;
b = i;
b = Boolean(i);

Because the value of i is a value other than zero, b is true.

The new coercion mechanism also works with many user-defined types. However, some coercions to or from user-defined types may not work because JScript may misinterpret the user's intent when converting dissimilar types. This is particularly true when the type that is converted from is composed of several values. For example, the following code creates two classes (types). One contains a single variable i that is an integer. The other contains three variables (s, f, and d), each of a different type. In the final statement, it is impossible for JScript to determine how to convert a variable of the first type to the second type.

class myClass {
   var i : int = 42;
}
class yourClass {
   var s : String = "Hello";
   var f : float = 3.142;
   var d : Date = new Date();
}
// Define a variable of each user-defined type.
var mine : myClass = new myClass();
var yours : yourClass;

// This fails because there is no obvious way to convert
// from myClass to yourClass
yours = yourClass(mine);

See Also

Concepts

Type Conversion

Coercion By Bitwise Operators

Other Resources

JScript Functions