“ drink ! Speak as if you understand , Surnamed Ye , You should talk first , How to create an array !” The disciple opposite said disdainfully , For arrays , Although he dare not say he is very proficient , But I know something . More importantly , I started a long time earlier than ye Xiaofan , Of course, I don't think I will be worse than ye Xiaofan . He's ready , Plan to interrupt Ye Xiaofan at any time .
“ Arrays can be defined in four ways .” Ye Xiaofan said at random .
“ what , Four kinds ? laugh off my head , It's the first time I've heard of it , Isn't an array defined by a pair of brackets , Where are the four ?” The disciple opposite smiled disdainfully .
“ You're talking about defining arrays with direct quantities .” Ye Xiaofan continued ,“ The so-called direct quantity definition , Is to declare an array object with a pair of square brackets , Like this ：”
var arr = ["first", "second", "third"]; console.log(arr);
“ Results obtained , Is to generate an array object with three elements , The name of the object is arr. The advantage of this method is when defining an array , Just initialize the array directly . In addition to this method , There are three other ways , Let me start with the second .” Say , Ye Xiaofan makes a code flow ：
var a = new Array();
var b = new Array(8); var c = new Array("first", "second", "third");
“ These three ways have their own differences , The first is to create an empty array directly with the constructor , in other words , There's nothing in this array . Arrays are born with one length attribute , I can make this a Variable calls its own length Property to verify this .”
var a = new Array(); console.log(a.length);
Code run , Everyone can see clearly , The result is a “0”.
“ I'm sure you've seen it , The result of this code is a 0, This means that there is nothing in the current array object . Next, let's look at the second way .”
var b = new Array(8);
“ This way is different from that just now , The difference is , Although it also creates an array , But it was created at the same time , Set an initial length , Let's see ,Array Is a function ,new Keyword represents the object represented by this function when it is created , Because it's a function , So naturally, you can use parentheses . you 're right , Functions can be parenthesized , Parentheses mean the body of the function to execute the function . Functions have parameters , this 8 That's the parameter . In this example ,8 It means adding an initialization length to the array object , I can still use the array length Property to verify this .” Say , Ye Xiaofan continues to type the code ：
var b = new Array(8); console.log(b.length);
Code run , Everyone can see clearly , The result is a “8”.
“ I'm sure you've seen it , The result of this code is a 8, This indicates that there are already in the current array object 8 Elements . So here comes the question... , I didn't add anything to this array , At the very least , It seems not . that , What are these eight elements ? I'll talk about this later , Look at the last way first .”
var c = new Array("first", "second", "third");
“ This method creates an array object at the same time , Give it an initial value . In short , That's when you create an array , Just add three elements to it . just because of this , So the current value of this array length Property already has a value , And it's the number of elements inside ：3!”
var c = new Array("first", "second", "third"); console.log(c.length);
Code run , Everyone can see clearly , The result is a “3”.
“ Let's continue with arrays , Just now I mentioned four ways to create arrays , The first is to create an array with a direct quantity , The other three are using constructors to create arrays . How to put it? , In fact, if you use it , The first method is the best , It's also the simplest way .”
“ Um , good point , About the creation of arrays , Yes, ye Xiaofan is .” Lin Yuanqing nodded with satisfaction . There was another uproar outside .
“ You talked about arrays just now length attribute , What's that? ?” The opposite disciple put away his contempt for ye Xiaofan , Asked frowning .
“ oh , You mean length attribute , Array has only one length attribute , namely length,length Represents the amount of memory space occupied by the array , Not just the number of elements in the array . for instance , I can define a length of 8 Array of , But there is only one element in it . It's like the code just now .”
var b = new Array(8);
“ variable b Point to an array , The memory space occupied by this array is 8 Units , in other words , have 8 There are two places where this array can hold other elements . Although I haven't added any elements to this array yet , But that doesn't mean the array has no length , And this length is the length of the array length attribute .”
“ What's inside this array ?”
“ Inside this array is 8 Empty elements , Nothing at all , But it takes up memory .” Say , Ye Xiaofan typed out a piece of code to verify .
var b = new Array(8); console.log(b);
Code run , Everyone can see clearly , The result is [ <8 empty items> ].
“ i see .” The disciples opposite also admire Qi Ye Xiaofan .
“ Array as an object , There are many built-in methods , Let's talk about those interesting methods .”