JavaScript defines initialization array functions _javascript tips

Source: Internet
Author: User
Tags arrays

Method one, the definition and initialization of JS array

Defining a one-dimensional array

Method 1:

var _thearray = new Array);//define
_thearray[0]= "1";
_thearray[1]= "2";
_thearray[2]= "3";
Class

Method 2:

var _thearray = new Array ("1", "2", "3");//directly defined and initialized

Defining two-dimensional arrays

Method 1

var _thearray = new Array ();  First declare one dimension
for (Var i=0;i<10;i++) {    //One dimension length of
 thearray[i]=new Array ();  In the declaration two-dimensional
 for (Var j=0;j<20;j++) {   //two-D length
 _thearray[i][j]=1
 }
}

Method 2

var _thearray = [["0-1", "0-2"],["1-1", "1-2"],["2-1", "2-2"]];//directly defined and initialized

The above code is tested under IE8


method Two,

<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 ( 
' Sunday ', 
' Monday ', 
' Tuesday ', 
' Wednesday ', 
' Thursday ', 
' Friday ', 
' Saturday ' 
); 
The Firfox browser displays the year by getyear () to get the current year minus 1900 
document.write ((Date.getyear () >1900?date.getyear ():d ate.getyear ( ) (+1900) + "year"); 
document.write (Date.getmonth () +1+ "Month"); 
document.write (Date.getdate () + "Day"); 
document.write (D[date.getday ()); 
</script>

Because JavaScript does not support arrays, the following code is used to define the initialization array.
function Initarray () {
This.length=initarray.arguments.length;
for (Var i=0;i<this.length;i++) {
This[i]=initarray.arguments[i];
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.