JavaScript教程-配列
3392 ワード
1,Aray定義配列/取得
1,var arr = new Array(); // arr
arr[0] = "tom";
arr[1] = "jim";
arr[2] = " ";
2,var a = new Array("tom", "jim", " ")
3,var str = [tom, jim, ];
str[0] //tom
str[1] /jim
str[2] /
2,Aray配列長さvar str = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var length = str.length //9
for if value
for( var i = 0;i
3,Aray配列は特別です. instanceof => // return true/false
var a = [ 1,2 ]
typeof a //object;
a instanceof Array //true
a instanceof String //false
4,Aray配列方法concat() // , 。
join() // 。 。
pop() //
push() // , 。
reverse() // 。
shift() //
slice() //
sort() //
splice() // , 。
toSource() // 。
toString() // , 。
toLocaleString() // , 。
unshift() // , 。
valueOf() //
実例js ( )
#shift: , ; undefined
var a = [1,2,3,4,5];
var b = a.shift(); //a:[2,3,4,5] b:1
#unshift: ,
var a = [1,2,3,4,5];
var b = a.unshift(-2,-1); //a:[-2,-1,1,2,3,4,5] b:7
: IE6.0 undefined,FF2.0 7, , splice 。
#pop: , ; undefined
var a = [1,2,3,4,5];
var b = a.pop(); //a:[1,2,3,4] b:5
#push: ,
var a = [1,2,3,4,5];
var b = a.push(6,7); //a:[1,2,3,4,5,6,7] b:7
#concat: ,
var a = [1,2,3,4,5];
var b = a.concat(6,7); //a:[1,2,3,4,5] b:[1,2,3,4,5,6,7]
#splice(start,deleteCount,val1,val2,...): start deleteCount , val1,val2,...
var a = [1,2,3,4,5];
var b = a.splice(2,2,7,8,9); //a:[1,2,7,8,9,5] b:[3,4]
var b = a.splice(0,1); // shift
a.splice(0,0,-2,-1); var b = a.length; // unshift
var b = a.splice(a.length-1,1); // pop
a.splice(a.length,0,6,7); var b = a.length; // push
#reverse:
var a = [1,2,3,4,5];
var b = a.reverse(); //a:[5,4,3,2,1] b:[5,4,3,2,1]
#sort(orderfunction):
var a = [1,2,3,4,5];
var b = a.sort(); //a:[1,2,3,4,5] b:[1,2,3,4,5]
#slice(start,end):
var a = [1,2,3,4,5];
var b = a.slice(2,5); //a:[1,2,3,4,5] b:[3,4,5]
#join(separator): , separator ,
var a = [1,2,3,4,5];
var b = a.join("|"); //a:[1,2,3,4,5] b:"1|2|3|4|5"
JavaScript , , (push)、 (shift) , for , JavaScript ?
JavaScript , 。 Object JS 。 Array Object :
Array:
# :var ary = new Array(); var ary = [];
# :ary.push(value);
# :delete ary[n];
# :for ( var i=0 ; i < ary.length ; ++i ) ary[i];
# :var obj = new Object(); var obj = {};
# :obj[key] = value; (key string)
# :delete obj[key];
# :for ( var key in obj ) obj[key];
Object , Popup
Web (3) Eric __MenuCache__, 。
Array , :