close

 

sample index :

1. 強轉

2. 隱轉

3. JSON 和 Array的區別

 

  1. 強轉 & 轉換  & NaN & 顯轉

 

 

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>無標題文件</title>

<script>

 

window.onload=function()

{

        var oTxt1=document.getElementById('txt1');

        var oTxt2=document.getElementById('txt2');

        var oBtn =document.getElementById('btn1');

         

        oBtn.onclick=function ()

        {

                //alert((oTxt1.value+oTxt2.value));

                //String to int - parseInt

                //String to float

                //NaN != NaN

                //NaN = Not a Number

                //isNaN: 用法檢測結果是否為NaN

               

                var n1 =parseFloat(oTxt1.value);

                var n2 =parseFloat(oTxt2.value);

                if (isNaN(n1))

                {

                        alert('您輸入的第一個數字有錯');

                }

                else if (isNaN(n2))

                {

                        alert('您輸入的第二個數字有錯');

                }

                else

                {

                        alert(n1+n2);

                }

               

               

                //alert(parseInt(oTxt1.value)+parseInt(oTxt2.value));

        }

}

</script>

</head>

 

<body>

<input id="txt1" type="text" />

<input id="txt2" type="text" />

<input id="btn1" type="button" value="求合">

</body>

</html>

 

 

 

隱轉

 

 

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>無標題文件</title>

<script>

var a = 5 ;

var b = '5';

//alert(a==b);          //true ( a轉成string,然後和b比較,所以才會是true);

// 雙等號 == : 是先轉換類型,然後比較

 

// 三等號 ===: 不轉換類型,直接比較

//alert(a===b);       //false

 

var c = '15';

var d = '10';

//alert(c+d);   output: 1015

//alert(c-d);     output: 5 !?有沒有很神奇,減法竟然是隱式轉換

 

//加法 : 1.字符串連接   2.數字相加

//減號 : 1.數字相減 ( 所以減法也是隱式類型轉換 )

 

</script>

</head>

 

<body>

</body>

</html>

 

 

 

 

JSON Array的區別

 

 

不一樣的地方

1. Json index 是用string的,

        Array的index是用 int 的。

2. ArraylengthJson則沒有

    Arr.length() ß

3. Array可以使用所有的loop方式,

      只能用for in的方式 (範例如下)

 

 

 

 

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>無標題文件</title>

<script>

 

var json={a:12,b:5,c:7};

var arr =[12,5,7];

/*

for (var i = 0 ; i<arr.length;i++)

{

        alert(''+i+'的值:'+arr[i]);   

}

*/

for (var i in json)

{

        alert(''+i+'的值:'+json[i]);  

}

 

 

 

</script>

</head>

 

<body>

</body>

</html>

 

 

 

 

arrow
arrow
    全站熱搜

    Eric 發表在 痞客邦 留言(0) 人氣()