JavaScript数组的3个属性和14个方法

属性
constructor:返回对创建此对象的数组函数的引用。
constructor属性是返回对创建此对象的数组函数的引用

var test=new Array(); if (test.constructor==Array) { document.write("This is an Array"); } if (test.constructor==Boolean) { document.write("This is a Boolean"); } if (test.constructor==Date) { document.write("This is a Date"); } if (test.constructor==String) { document.write("This is a String"); }

输出结果为:
This is an Array
length:设置或返回数组中元素的数目。
length属性是设置或返回数组中的元素的数目
var arr = new Array(3) arr[0] = "John" arr[1] = "Andy" arr[2] = "Wendy"document.write("Original length: " + arr.length) document.write("
")arr.length=5 document.write("New length: " + arr.length)

输出结果为:
Original length: 3
New length: 5
prototype:使您有能力向对象添加属性和方法。
prototype属性是相对想添加属性和方法
function employee(name,job,born) { this.name=name; this.job=job; this.born=born; }var bill=new employee("Bill Gates","Engineer",1985); employee.prototype.salary=null; bill.salary=20000; document.write(bill.salary);

输出结果为:
20000
方法
concat():连接两个或更多的数组,并返回结果。
concat()方法是连接两个或者多个的数组,并返回结果
var a = [1,2,3]; document.write(a.concat(4,5));

输出结果为:
1,2,3,4,5
join():把数组的所有元素放入一个字符串。前端培训元素通过指定的分隔符进行分隔。
join()方法是把数组的所有元素放入一个字符串,通过指定的分隔符进行分隔
var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas"document.write(arr.join())

输出结果为:
George,John,Thomas
pop():删除并返回数组的最后一个元素
pop()方法是删除并返回数组的最后一个元素
var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas"document.write(arr)document.write("
")document.write(arr.pop())document.write("
")document.write(arr)

输出结果为:
George,John,Thomas
Thomas
George,John
push():向数组的末尾添加一个或更多元素,并返回新的长度。
push()方法是向数组的末尾添加一个或更多元素,并返回新的长度。
var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas"document.write(arr + "
") document.write(arr.push("James") + "
") document.write(arr)

【JavaScript数组的3个属性和14个方法】输出结果为:
George,John,Thomas
4
George,John,Thomas,James
reverse():颠倒数组中元素的顺序。
reverse()方式是颠倒数组中元素的顺序
var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas"document.write(arr + "
") document.write(arr.reverse())

输出结果为:
George,John,Thomas
Thomas,John,George
shift():删除并返回数组的第一个元素
shift()方法是删除并放回数组的第一个元素
var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas"document.write(arr + "
") document.write(arr.shift() + "
") document.write(arr)

输出结果为:
George,John,Thomas
George
John,Thomas
slice():从某个已有的数组返回选定的元素
slice()方法是从某个已有的数组返回选定的元素
var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas"document.write(arr + "
") document.write(arr.slice(1) + "
") document.write(arr)

输出结果:
George,John,Thomas
John,Thomas
George,John,Thomas
sort():对数组的元素进行排序
sort()方式是对数组的元素进行排序
var arr = new Array(6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "James" arr[4] = "Adrew" arr[5] = "Martin"document.write(arr + "
") document.write(arr.sort())

输出结果为:
George,John,Thomas,James,Adrew,Martin
Adrew,George,James,John,Martin,Thomas
splice():删除元素,冰箱数组添加新元素
splice()方法是删除元素,并向数组添加新元素
举例:创建一个新数组,并将其添加一个元素
var arr = new Array(6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "James" arr[4] = "Adrew" arr[5] = "Martin"document.write(arr + "
") arr.splice(2,0,"William") document.write(arr + "
")

输出结果为:
George,John,Thomas,James,Adrew,Martin
George,John,William,Thomas,James,Adrew,Martin
举例:删除位于i[2]的元素,并添加一个新的元素来替换被删除的元素
var arr = new Array(6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "James" arr[4] = "Adrew" arr[5] = "Martin"document.write(arr + "
") arr.splice(2,1,"William") document.write(arr)

输出结果为:
George,John,Thomas,James,Adrew,MartinGeorge,John,William,James,Adrew,Martin
toSource():返回该对象的源代码
toSource()方法是返回该对象的源代码
function employee(name,job,born) { this.name=name; this.job=job; this.born=born; }var bill=new employee("Bill Gates","Engineer",1985); document.write(bill.toSource());

输出结果为:
({name:"Bill Gates", job:"Engineer", born:1985})
toString():把数组转换为字符串,并返回结果
toString()方法是把数组转换为字符串,并返回结果
var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas"document.write(arr.toString())

输出结果为:
George,John,Thomas
toLocaleString():把数组转换为本地数组,并返回结果
toLocaleString()方法把数组转换为本地数组,并返回结果
var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas"document.write(arr.toLocaleString())

输出结果为:
George, John, Thomas
unshift():像数组的开头添加一个或者更多的元素,并且返回性的数组长度
unshift()方法是像数组的开头添加一个或者更多的元素,并且返回性的数组长度
var arr = new Array() arr[0] = "George" arr[1] = "John" arr[2] = "Thomas"document.write(arr + "
") document.write(arr.unshift("William") + "
") document.write(arr)

输出结果:
George,John,Thomas
William,George,John,Thomas
valueOf():返回数组对象的原始值。
valueOf()方法返回数组对象的原始值。
语法:
arrayObject.valueOf()

    推荐阅读