Javascript的类型转换
对于Javascript来说,变量也是有类型的。看下面的小例子
这是测试的html
<p>
<input type="text" name="txtVal"/>
<input type="button" name="btnTest" value="测试" onclick="test();"/>
</p>
<span id="lblTest">测试结果</span>看一下test方法
function test()
{
var val;
var result;
var str='';
val = document.all.txtVal.value;
str = '测试结果<br>';
str += '1+val=';
result = 1+val;
str += result;
str += '<br>1+parseInt(val)=';
result = 1+ parseInt(val);
str+=result;
document.all.lblTest.innerHTML=str;
}结果显示
测试结果
1+val=11
1+parseInt(val)=2
function test()
{
var val;
var result;
var str='';
val = document.all.txtVal.value;
str = '测试结果';
val++;
str += '<br>val++ 后val='
str += val;
str += '<br>1+val=';
result = 1+val;
str += result;
str += '<br>1+parseInt(val)=';
result = 1+ parseInt(val);
str+=result;
document.all.lblTest.innerHTML=str;
}新的结果
测试结果
val++ 后val=2
1+val=3
1+parseInt(val)=3
类型转换有时还是明显的执行为妙,以防出错。
这是测试的html
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
很明显val的类型是字符串。Javascript不是会自动转换类型吗?是的!这是肯定的。上面的例子,如果在所有加法运算前多加一句val++,结果就不同了。新的test 方法
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
类型转换有时还是明显的执行为妙,以防出错。