1.创建数组
var arr = [];
var arr = new Array();
var arr =new Array("1","2","3");
2.forEach遍历
var fn = new Array("1","2","3");
fn.forEach(function(item,index){
console.log(item,index);
});
//1 ,0
// 2, 1
//3 ,2
3.for循环
var fn = new Array("1","2","3");
for(let i = 0; i< fn.length; i++){
console.log(fn[i])
};
4.for of 循环,会把数组中值的类型直接输出
var fn = new Array("a","b","c");
for(let i of fn){
console.log(i);
};
//a
//b
//c
5.for in 遍历 会将数组的下标输出出来
var fn = new Array("a","b","c");
for(let i in fn){
console.log(i);
};
//0
//1
//2
6.显示源代码toSource()此方法只支持火狐,其他浏览器暂不支持。
function Body(name,age,height){
this.name=name;
this.age= age;
this.height= height;
};
var newbody = new Body("小明","18","178cm");
var newbodyFunction = newbody.toSource();
console.log(newbodyFunction)
7.map遍历
var fn = new Array("a","b","c");
fn.map(function(x,y){
console.log(x,y);
});
//a 0
//b 1
//c 2
遍历开方返回新数组,原数组不变
var fn = new Array("4","16","25");
var fnn = fn.map(Math.sqrt);
console.log(fnn);
//[2, 4, 5];