javascriptあれらのこと(一)javascript配列の使い方のまとめ(1)


/*
1、js            ,         ,     ,js                 。
  js             。

2、      :
*/

//(1)  Array        :
//var names = new Array();

//  length=30   
//var names = new Array(30); //          ,        
//alert(names.length);  //30

//    Array              :
//var names2 = new Array("zhangsan","lisi","wangwu");
//alert(names2); //zhangsan,lisi,wangwu

// Array                
//var ages = new Array(2);  //         
//var ages = new Array("11"); //    1 ,    “11”   

//    new   
//var books = Array(2);//         
//var books = Array("books1");    1 ,    “books”   


//(2)               :                   ,            
//var names = ["zhangsan","lisi","wangwu"];  //      3       
//var names = []; //        

//              :

//            2      "zhangsan","lisi"   
// IE ,names     3 ,    “zhangsan","lisi",undefined   ,
//    IE8      ECMAScript            BUG
//var names = ["zhangsan","lisi",];
//alert(names); //IE9   ,IE8   :zhangsan,lisi,
//alert(names.length);


//         ,     undefined 。
//var names = [,,,,,];
//alert(names.length); //5:            5    
//alert(names.length);  // 6:   IE       6    
//alert(names[5]); //undefined


//       
//var colors = ["red","green","blue"];
//alert(colors[0]);  //     
//colors[2] = "pink";  //     
//alert(colors); // red,green,pink

//colors[3] = "black"; //     ,               ,        1
//alert(colors); //red,green,pink,black


//3、length  :>=0  ,length       ,      length                   
//var colors = ["red","green","blue"];
//colors.length = 2;  
//alert(colors[2]);  //undefined

//var colors = ["red","green","blue"];
//colors.length = 4;  
//alert(colors[3]);  //undefined

//var colors = ["red","green","blue"];
//colors[colors.length] = "black";
//colors[colors.length] = "pink";
//alert(colors);  //red,green,blue,black,pink

//var colors = ["red","green","blue"]
//colors[99] = "black";
//alert(colors.length); // 100  ,colors[3] color[98]  undefined

//4、       

//              ,instanceof           
//var colors = ["red","green","blue"];
//var obj = {};
//alert(colors instanceof Array); //true
//alert(obj instanceof Array); // false

//           ,               ,           Array    ,
//                   ,                         
//        ,    Array.isArray()  

//var colors = ["red", "green", "blue"];
//var obj = {};
//alert(Array.isArray(colors)); //true
//alert(Array.isArray(obj)); //false

//   :  isArray       : IE9+,     


//5、toString():                 ,        toString()  
//valueOf():     
//var colors = ["red", "green", "blue"];
//alert("toString="+colors.toString()); //red,green,blue
//alert(colors); //alert       ,        ,        Array toString()  
//alert("valueOf="+colors.valueOf()); 
//alert(Array.isArray(colors.valueOf())); //true

//toLocaleString():                  ,      toLocaleString()  ,   toString()  
/*
var book1 = {
	toString : function(){
		return "book1.toString()";
	},

	toLocaleString : function(){
		return "book1.toLocaleString()";
	}

};
var book2 = {
	toString : function(){
		return "book2.toString()";
	},

	toLocaleString : function(){
		return "book2.toLocaleString()";
	}

};
var book = [book1,book2];
alert(book); //book1.toString(),book2.toString()
alert(book.toString); //function toString(){[natice code]}
alert(book.toString());//book1.toString(),book2.toString()
alert(book.toLocaleString());//book1.toLocaleString(),book2.toLocaleString()
*/

//join():             ,             ,
//var colors = ["red^", "green^", "blue^"];
//alert(colors.join(','));//red^,green^,blue^
//alert(colors.join('|'));//red^|green^|blue^
//alert(colors.join()); //       : red^,green^,blue^
//alert(colors.join(undefined));// IE8+         :red^,green^,blue^
//alert(colors.join(undefined)); // IE7       :red^undefinedgreen^undefinedblue^

//           null undefined,    join()\valueOf()\toString()\toLocaleString()       
//var names = ["zhangsan",null,undefined,"lisi"];
//alert(names); //zhangsan,,,lisi


//push():          ,       ,         
//var names = new Array();
//var num = names.push("zhangsan","lisi");
//alert(num); //2
//num = names.push("wangwu");
//alert(num); //3
//var item = names.pop();
//alert(item); //wangwu

//shift() :           
//var colors = ["red","green","blue"];
//var item = colors.shift();  //        
//alert(item);
//alert(colors.length); //2

//unshift() :                    ,  IE7     ,unshift  undefined       
//var colors = ["red","green","blue"];
//var count = colors.unshift("aaa","bbb");
//alert(colors);//"aaa","bbb","red","green","blue"
//alert(count);//5