令人迷惑的Javascript

最近熟悉了一下javascript,发现的确很强大。但是也很诡异。

javascript的下标竟然从1开始。不过还好我可以接受。但有时它又从0开始。疯了!

用过javascript的人都知道它的数组下标是可以从1开始。但是它有时也从0开始,看下面的代码


var strr = " 1,2,3,4;5,6,7,8; "


function strSplit(str)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBloc
kStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/
ContractedBlock.gif) … {

var ss = str.split( ’ ; ’ );

for (var i in ss)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicato
rs/ContractedSubBlock.gif) … {


var sss = ss[i].split( ’ , ’ );

if (sss != “” )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicato
rs/ContractedSubBlock.gif) … {

for (var j = 0 ; j < sss.length; j ++ ) // 注意这里是从0开始的
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicato
rs/ContractedSubBlock.gif) … {

alert(sss[j]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockEnd.gif) }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockEnd.gif) }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockEnd.gif) }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBloc
kEnd.gif) }


strSplit(strr);

如果从1下标开始,那就开始只打印2。

所以说javascript真好,可以充分锻炼自己的找bug与排除bug的能力。

所以以后大家数组定一定的时候一定要从0下标开始。

javascript的For…in可以蔽掉所有的undefine对象。嗯,这点很好。但是它可能并不按照数组的已有顺序!

下面是一个把元素插入到数组中代码


function insert(array,i,value)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBloc
kStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/
ContractedBlock.gif) … {

var index = i;

for (;index <= array.length;index ++ )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicato
rs/ContractedSubBlock.gif) … {

if (array[index] == null )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicato
rs/ContractedSubBlock.gif) … {//这里是找到一个空位置

break ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockEnd.gif) }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockEnd.gif) }


for ( var j = index; j > i;j -- )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicato
rs/ContractedSubBlock.gif) … {//这里把现有数组的元素向后移

array[j] = array[j - 1 ];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockEnd.gif) }

array[i] = value;

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBloc
kEnd.gif) }


var o = new Array;
o[
1 ] = 1 ;
o[
3 ] = 2 ;

insert(o, 1 , 3 );//在1的位置插入3

var str = “” ;

for ( var x in o)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBloc
kStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/
ContractedBlock.gif) … {

str += o[x];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBloc
kEnd.gif) }

str += " ! "

for ( var x = 1 ; x < o.length; x ++ )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBloc
kStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/
ContractedBlock.gif) … {

if (o[x] != null )

str += o[x]

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBloc
kEnd.gif) }

alert(str);//打印结果是 321!312

我是解释不通javascript是怎么遍历的。但的确不是按照顺序遍历的