Home >Web Front-end >JS Tutorial >javascript defines initialization array function_javascript skills

javascript defines initialization array function_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:47:111387browse

Method 1, definition and initialization of js array

Define one-dimensional array

Method 1:

var _TheArray = new Array);//定义
_TheArray[0]="1";
_TheArray[1]="2";
_TheArray[2]="3";
//初始化

Method 2:

var _TheArray = new Array("1","2","3");//Directly define and initialize

Define a two-dimensional array

Method 1

var _TheArray = new Array();  //先声明一维
for(var i=0;i<10;i++){    //一维长度为10
 TheArray[i]=new Array();  //在声明二维
 for(var j=0;j<20;j++){   //二维长度为20
 _TheArray[i][j]=1;
 }
}

Method 2

var _TheArray = [["0-1","0-2"],["1-1","1-2"],["2-1","2-2"]]; //Define and initialize directly

The above code passed the test under IE8


Method 2,

<script type="text/javascript"> 
var date=new Date(); 
function initArray(){ 
this.length=initArray.arguments.length; 
for(var i=0;i<this.length;i++){ 
this[i]=initArray.arguments[i]; 
} 
} 
var d=new initArray( 
'星期日', 
'星期一', 
'星期二', 
'星期三', 
'星期四', 
'星期五', 
'星期六' 
); 
//Firfox浏览器显示年是由getYear()获取当前年减去1900年 
document.write((date.getYear()>1900&#63;date.getYear():date.getYear()+1900)+"年"); 
document.write(date.getMonth()+1+"月"); 
document.write(date.getDate()+"日 "); 
document.write(d[date.getDay()]); 
</script>

Because JavaScript does not support arrays, you need to use the following code to define the initialization array.
function initArray(){
this.length=initArray.arguments.length;
for(var i=0;i this[i]=initArray.arguments[i];
}
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn