?????? ?? ??
Javascript ??? ??? ????? Javascript? ???? ?? ??? ??? ??? ? ??? ?? ??? ??? ? ???? ??? ? ????. ?? ??????.
var n = 10;
n = "hello CSSer!";
n = {};
? ??? n ??? ?? ???? ?? ?? 10(?? ??)?? ???? ?? "hello CSSer!"?? ???? n? ??? ?? ??? ?????. ????? n? ??? ?? ?????. ?? n? ??? ???? ? ? ???, ?? ???????? ??? ??? ?? ???? ?? ?? ????. ???? ?? ?? ?????.
JavaScript ??? ??
JavaScript?? 5?? ??? ??? ????:
string
number
boolean
object
function
3?? ?? ??:
Object
??
??
?? ?? ???? ?? 2?? ??? ??:
null
undefine
typeof ???
typeof ???? ???? JavaScript ??? ??? ??? ? ? ????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = typeof "tom" + "<br>" + typeof 3.14 + "<br>" + typeof NaN + "<br>" + typeof false + "<br>" + typeof [1,2,3,4] + "<br>" + typeof {name:'john', age:34} + "<br>" + typeof new Date() + "<br>" + typeof function () {} + "<br>" + typeof myCar + "<br>" + typeof null; </script> </body> </html>
??:
NaN? ??? ??? ?????
??(Array)? ??? ??? object
??(Date)? ??? ??? object
null ??? ??? object
???? ?? ?? ??? ??? ???? ?????
constructor ??
constructor ??? ?? JavaScript ??? ???? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = "john".constructor + "<br>" + (3.14).constructor + "<br>" + false.constructor + "<br>" + [1,2,3,4].constructor + "<br>" + {name:'john', age:34}.constructor + "<br>" + new Date().constructor + "<br>" + function () {}.constructor; </script> </body> </html>
??? ??
???? ?? ??? ???? Javascript? ??? ?? ?? ??? ?????. )
??? ???? ??: toString(radix), String(mix)Boolean ???? ??: Boolean(mix)
1. Number(mix) ??? ?? ??? ???? ??? ?? ???? ??? ? ????
??? ??? ????.
?? ?? ?? true? false? ?? 1? 0?? ?????
?? ?? ?? ????? ?????.
null?? 0? ?????.
???? ??? NaN? ?????.
???? ?? ?? ??? ????.
1) ???? ??? ??? ?? 10??? ?????(?? 0 ??).
2) ???? ??? ?? ??? ??? ??? ?? ?????. ?? ??? ??? ?? (?? 0? ??)
3) ? ????? 0?? ??
4) ???? ?? ?? ??? ???? ??? NaN?? ??
?? ??? ?? ??? valueOf() ???? ??? ?? ?? ??? ?? ??? ?? ?????. ?? ??? NaN? ?? ??? toString() ???? ???? ??? ??? ?? ?? ??? ?? ?? ?????.
Number("3.14") // 3.14? ??
Number(" ") // 0? ??
Number("") // 0? ??
Number("99 88") // NaN
? ??
2.parseInt(string, radix) ??? ???? ??? ??? ?????. ?? ?? ??? ????:
? ?? ?? ?? ?? ??? ?? ??? ??? ?? ??? ?????.
? ?? ??? ?? ??? ?? ??? ?? ?? NaN? ?????
If ? ?? ??? ??? ?? ??? ?? ??? ????? ??? ?? ??? ??? ??? ?? ??? ?????.
?? ?? ?? ??? 0?? ???? 0x? ???? 8??? ?? ?????. , 16??? ?? ??
radix ????? ???? radix
3? ???? ?? ?????.parseFloat(string) ???? ???? ?? ??? ?? ?
?? ?????. ??? ????? parInt? ????? ? ?? ???? ????. ???? ? ?? ??? ??? ????,parseFloat? ?? ?? 0? ?????. ???? ??? ?? ??? ? ?? ??? ???? ??? ?? ?? ??? ????. ?? ??? ?? ?? ?????.
4.toString(radix) ???. ???? ?? ? null? ??? ?? ??? ??? ??? ??? ??? ???? toString() ???? ????.
?? ??
Array Array? ??? ???? ?????. ?? ???? ??? ???? ?????.
Boolean ?? ?? true?? "true"? ?????. ??? ??? "false"? ?????.
Date ??? ??? ??? ?????.
Error ?? ?? ??? ??? ???? ?????.
Function ?? ???? ???? ?????. ??? functionname? ??? toString ??? ??? ?????.
function functionname() { [???? ??] }
Number ??? ??? ??? ?????.
String String ??? ?? ?????.
Default? "[object objectname]"? ?????. ??? objectname? ?? ??? ?????.
5. ?? ??? ?? ???? ???? ?????. ??? ??? ????.
toString() ???? ?? ?? ??? ???? ?? ? ???? ?????. ???? ) ??? ????
null?? "null"? ?????.
???? ??? "undefine"? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <p id="demo"></p> <script> var x = 55; document.getElementById("demo").innerHTML = String(x) + "<br>" + String(13.3) + "<br>" + String(100.99 + 23); </script> </body> </html>
6 ??(??) ??, ?? ??? ?? ?????. ?? ??? ?????.
?? ?? false? ?????. false, "", 0, NaN, null, ???? ?? ? ?? ?? ?? true? ?????.
?:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script type="text/javascript"> var i="123abc"; i=parseInt(i);//字符串轉(zhuǎn)整形 alert(i+","+typeof(i));//輸出:123,number i="12.3abc"; i=parseFloat(i);//字符串轉(zhuǎn)浮點(diǎn)型 alert(i+","+typeof(i));//輸出:12.3,number(可見(jiàn)不管是int還是float都是number類型) i="a123abc"; i=parseInt(i);//字符串轉(zhuǎn)整形 alert(i+","+typeof(i));//輸出:NaN,number (由于轉(zhuǎn)換失敗,所以提示“不是一個(gè)數(shù)字,Not a Number”) var num=document.getElementById("num").value; function showMsg(num) { for(var i=0;i<num;i++) { document.write("你好,JavaScript!<br/>"); } } </script> </head> <body> </body> </html>
?? ??? +
??? +? ???? ??? ??? ??? ? ????:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <button onclick="myFunction()">點(diǎn)擊</button> <p id="demo"></p> <script> function myFunction() { var y = "5"; var x = + y; document.getElementById("demo").innerHTML = typeof y + "<br>" + typeof x; } </script> </body> </html>