Array 对象方法

本文深入探讨了JavaScript中数组的各种操作方法,包括数组的构造、属性设置、连接、转换及多种实用函数的应用,如push、pop、concat、join、slice等,并通过实例演示了这些方法的具体用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<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)+"&nbsp;&nbsp;");

        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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值