tailieunhanh - Học php, mysql và javascript - p 34

Biến Typing Cũng như PHP, JavaScript là một ngôn ngữ rất lỏng lẻo đã gõ, các loại biến được xác định chỉ khi một giá trị được giao và có thể thay đổi khi biến xuất hiện trong các ngữ cảnh khác nhau. | Character Meaning XXX Backslash An octal number between 000 and 377 that represents the Latin-1 character equivalent such as 251 for the symbol xXX A hexadecimal number between 00 and FF that represents the Latin-1 character equivalent such as xA9 for the symbol uXXXX A hexadecimal number between 0000 and FFFF that represents the Unicode character equivalent such as u00A9 for the symbol Variable Typing Like PHP JavaScript is a very loosely typed language the type of a variable is determined only when a value is assigned and can change as the variable appears in different contexts. Usually you don t have to worry about the type JavaScript figures out what you want and just does it. Take a look at Example 14-8 in which 1. The variable n is assigned the string value 838102050 the next line prints out its value and the typeof operator is used to look up the type. 2. n is given the value returned when the numbers 12345 and 67890 are multiplied together. This value is also 838102050 but it is a number not a string. The type of variable is then looked up and displayed. 3. Some text is appended to the number n and the result is displayed. Example 14-8. Setting a variable s type by assignment script n 838102050 Set n to a string n n and is a typeof n br n 12345 67890 Set n to a number n n and is a typeof n br n plus some text Change n from a number to a string n n and is a typeof n br script The output from this script looks like n 838102050 and is a string n 838102050 and is a number n 838102050 plus some text and is a string If there is ever any doubt about the type of a variable or you need to ensure a variable has a particular type you can force it to that type using statements such as the following which respectively turn a string into a number and a number into a string Variable Typing 311 n 123 n 1 Convert n into a number n 123 n Convert n into a string Or of course you can always look up a variable s type using the typeof