Ò׽ؽØÍ¼Èí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

javascript parseInt is broken

javascript parseInt is broken
I was debugging some strange errors in a date conversion function I was writing, and I stumbled upon something that amazed me... a strange bug in parseInt
>>>parseInt('06')
6
>>>parseInt('07')
7
>>>parseInt('08')
0
>>>parseInt('09')
0
>>>parseFloat('08')
8
>>>parseFloat('09')
9
 
Clearly for the strings '08' and '09' parseInt fails to return the right value. One workaround is easy, use parseFloat.
Why does this happen and where should I report this? I find it extremely odd that it happens in both Firefox and Internet Explorer 6 & 7; there must be some explanation right?
You can test it out for yourself using the FireBug javascript console), or by using one of these two links
alert(parseInt('07')+' == 7 ?')
alert(parseInt('08')+' == 8 ?')
Ok, Ok, so now that I explained how you would expect it to work (and clearly how I expected it to work) I'm going to answer my own question (yes I hate it when I do that too)
The problem is with how parseInt guesses the base of your number. Read the parseInt spec. Instead of always defaulting to base 10, it tries to guess, and if the first character is '0' it thinks you want to parse as an octal number, and if it starts with '0x' it thinks you want hexadecimal.
So, the solution is either to use parseFloat or to always specify your base.
Defaults that change on their own can't be trusted.
>>>parseInt('08',10)
8
 


Ïà¹ØÎĵµ£º

JavaScriptÅÅÐòËã·¨

1¡¢²åÈëÅÅÐò->Ö±½Ó²åÈë·¨ÅÅÐò
function InsertSort(arr) { //²åÈëÅÅÐò->Ö±½Ó²åÈë·¨ÅÅÐò
var st = new Date();
var temp, j;
for(var i=1; i<arr.length; i++) {
if((arr[i]) < (arr[i-1])) {
temp = arr[i];
j = i-1;
do {
arr[j+1] = arr[j];
j--;
}
whil ......

flex ºÍjavascript»¥Ïàµ÷ÓÃ

flex ºÍjavascript»¥Ïàµ÷Óá£
Ò»¡¢flex µ÷ÓÃjavascript
  Ö»ÐèʹÓþ²Ì¬µÄExternalInterface.call¾Í¿ÉÒÔÁË¡£
  call ·½·¨ÖÁÉÙÒªÇóÒ»¸ö²ÎÊý£¬¾ÍËùµ÷ÓöÊýµÄÃû³Æ¡£
  Ò²¿ÉÒÔ´«µÝ²ÎÊý¡£
  È磺
    ExternalInterface.call("alert","hello");
   "hello"¾ÍÊÇ×÷Ϊalertµ ......

javascript ÔËÐлúÖÆ

ÒÔÏÂÈ«ÊǸöÈËÀí½âÒÔ¼°ÍøÉϲéÕÒ¶øÀ´,ÈçÓв»¶ÔÇëÖ¸Õý...
¼ÙÈçÓÐn¶Îjs´úÂë ÓÃ<script>±êÇ©¸ô¿ªµÄ.
ÔËÐÐ˳ÐòÊÇ
step1. ¶ÁÈëµÚÒ»¸ö´úÂë¶Î
step2. ×öÓï·¨·ÖÎö£¬ÓдíÔò±¨Óï·¨´íÎ󣨱ÈÈçÀ¨ºÅ²»Æ¥ÅäµÈ£©£¬²¢Ìø×ªµ½step5
step3. ¶Ôvar±äÁ¿ºÍfunction¶¨Òå×ö“Ô¤½âÎö”£¨ÓÀÔ¶²»»á±¨´íµÄ£¬ÒòΪֻ½âÎöÕýÈ·µÄÉùÃ÷£©
......

JavaScript¹ýÂËÊý×éÖÐÖØ¸´ÔªËØ

JavaScript¹ýÂËÊý×éÖÐÖØ¸´ÔªËØ
ÎÒÊǸöJS³õѧÕߣ¬ÎÒ¼´½«ÒªËµµÄÕâ¸ö·½·¨Ò²ÊǴ󲿷ÖÈ˶¼ÄÜÏëµ½µÄ£º
´Ó¾ÉÊý×éÖÐÈ¡ÔªËØ£¬Ò»¸ö¸öÌí¼Óµ½ÐÂÊý×éÖУ¬ÔÚÌí¼ÓµÄʱºò£¬ÓëÌí¼Ó¹ýµÄÔªËØ±È½Ï£¬Èç¹ûÏàͬ£¬Ôò²»Ìí¼Ó¡£
Ê×Ïȶ¨ÒåÁ½¸öÊý×飺
Code
var arrA = new Array(1,23,43,64,1,23,5,8,3,5,9);
var arrB&n ......

JavascriptʵÏÖÈÕÆÚʱ¼äµÄµ¹¼ÆÊ±

<html>
<body>
<SCRIPT type="text/javascript">
<!--
var target=[]
var time_id=[]
function ShowDateTime(){
    setTimeout("ShowDateTime()", 1000);
for (var i=0,j=target.length;i<j;i++)
{
    var today=new Date();
    timeo ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ