04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 22: The Evolution of JavaScript<br />

Strings may be declared as being the type string or String . The only difference between these two is<br />

that variables declared as string cannot be set to null , whereas variables declared as String can. Here<br />

is an example:<br />

var name : string = “Nicholas”;<br />

var anotherName : string = null;<br />

//error!<br />

var title : String = “Professional JavaScript”;<br />

var publisher : String = null; //OK<br />

In this example, the variables name and anotherName cannot be set to null ; if you attempt to make<br />

them so, an error occurs. If a variable declared as String is not initialized, then it is given the value of<br />

null , which is different from ECMAScript 3 where uninitialized variables always hold the value<br />

undefined .<br />

The same holds true for Boolean values that may be declared as either boolean or Boolean . When a<br />

variable is declared as boolean , it cannot be set to null , whereas Boolean variables can be set to null .<br />

Generally, types that begin with a lowercase letter aren ’ t nullable, whereas those beginning with an<br />

uppercase letter are.<br />

The biggest difference between types in ECMAScript 3 and 4 comes with numbers. In ECMAScript 3, all<br />

numbers are represented by the Number type. ECMAScript 4 introduces the following, more specific<br />

number types:<br />

❑<br />

❑<br />

❑<br />

❑<br />

int — Whole numbers in the range - 2147483648 through 2147483647.<br />

uint — Whole numbers in the range 0 through 4294967295.<br />

double — 64 - bit IEEE binary floating - point values; effectively the same as primitive numbers in<br />

ECMAScript 3.<br />

decimal — 128 - bit IEEE binary floating - point values. Arithmetic with these values is more<br />

precise than with double .<br />

None of these data types are nullable, though there is a Number type that can be used when null is a<br />

valid option. The Number type is the equivalent of Number in ECMAScript 3 and contains a nullable<br />

64 - bit IEEE binary floating - point value. Here are some examples:<br />

var length : Number = 5;<br />

var size : Number = null;<br />

var area : Number = 125.9;<br />

var age : uint = 29;<br />

var distance : double = 54.3;<br />

var average : decimal = 1.23;<br />

var temperature : int = -32;<br />

Numeric literals are interpreted to be specific data types. Any hexadecimal literal (beginning with 0x) is<br />

considered to be a uint , whereas most whole numbers are considered int . If a positive number is in the<br />

range for uint but not in the range of int , the number becomes a uint . All other values are considered<br />

to be doubles for backwards compatibility with ECMAScript 3. You can force the interpreter to treat a<br />

718

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!