<html>
<title>无标题文档</title>
</head>
<body>
<script>
//constructor 返回对创建此对象的数组函数的引用
var test=new Array();
if(test.constructor==Array){
document.write("This is a Array");
}
if(test.constructor==Boolean){
document.write("This is a Boolen")}
if(test.constructor==Date){
document.write("This is a Date");
}
if(test.constructor==String){
document.write("This is a String");
}
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.constructor);
//设置或返回数组中元素的数目 length
var arr=new Array(3);
arr[0]="a";
arr[1]="b";
arr[2]="c";
document.write(arr.length);
document.write("<br>");
arr.length=5;
document.write(arr.length);
//向对象中添加属性 prototype
function employee2(name,job,born){
this.name=name;
this.job=job;
this.born=born;
}
var bill2=new employee2("l","s",1999);
employee2.prototype.salary=null;
bill2.salary=200000;
document.write(bill2.salary);
//concat()方法用于连接两个或多个数组,该方法不会改变现有的数组,而仅仅会返回连接数组的一个副本
var a=[1,2,3];
document.write(a.concat(4,5));
document.write("<br>");
var b=new Array(3);
b[0]="a";
b[1]="b";
b[2]="c";
var c=new Array(3);
c[0]="a";
c[1]="b";
c[2]="c";
document.write(b.concat(c));
document.write("<br>");
document.write(b);
document.write("<br>");
document.write(c);
document.write("<br>");
document.write(a.concat(b,c));
document.write("<br>");
//把数组中的所有元素放入一个字符串中。元素通过指定的分隔符进行分隔
document.write(b.join());
var s=b.join();
document.write(s.constructor);
document.write("<br>");
document.write(b.join("*"));
document.write("<br>");
//pop()方法删除数组的最后一个元素,把数组长度减1,并且放回它删除的元素值,如果数组已经唯恐,则pop()不改变数组,并且返回undefined值
document.write(b.pop());
document.write(b.pop());
document.write(b);
document.write("<br>");
//push()方法可像数组的末尾添加一个或多个元素,并返回行的长度
document.write(c.push("d"));
document.write("<br>");
//reverse()方法用于颠倒数组中的元素数序
document.write(c.reverse());
document.write("<br>");
//shift()方法删除并返回数组中的第一个元素
document.write(c.shift());
document.write("<br>");
//slice()方法从某个已有的数组返回选定的元素
document.write(c.slice(1)+" ");
document.write(c.slice(-1));
document.write(c.slice(-2));
document.write("<br>");
//从下标1开始但不包括结束下标
document.write(c.slice(1,2));
document.write("<br>");
//sort()方法对数组进行排序
var aa=new Array(3,5,2,8,6,4);
document.write(aa.sort());
document.write("<br>");
//splice()方法输出元素,并向元素中添加新元素
aa.splice(2,0,100);
document.write(aa);
aa.splice(2,3,500);
document.write(aa);
document.write("<br>");
var aas=new Array(110,120,119);
aa.splice(2,1,aas);
document.write(aa);
document.write("<br>");
//把数组转换成字符串,并返回结果
document.write(aas.toString());
//把数组转换成本地数组,并返回结果
document.write("<br>");
//doucment.write(aas.toLocaleString());
document.write("<br>");
//unshift()方法像数组开头添加一个或更多元素,并返回新的长度
document.write(aas.unshift(1,2,3,4));
//返回数组对象的原始值
document.write("<br>");
document.write(aas.valueOf());
document.write("<br>");
document.write("<br>");
document.write("<br>");
document.write("<br>");
document.write("<br>");
document.write("<br>");
</script>
</body>
</html>
Array 对象方法
最新推荐文章于 2024-09-09 16:30:33 发布