• JavaScript【引用方法】操作方法


     1 <script>
     2     var arr1 = ["a","b","c",undefined,null];
     3     //concat()
     4     //创建数组副本,将接收到的参数添加到副本末尾
     5     var arr2 = arr1.concat("d","e");
     6     console.log(arr1);    //输出:(5) ["a", "b", "c", undefined, null]
     7     console.log(arr2);    //输出:(7) ["a", "b", "c", undefined, null, "d", "e"]
     8 
     9     //slice()
    10     //返回指定位置的项
    11     //接收1个参数
    12     console.log(arr1.slice(1));         //输出:(4) ["b", "c", undefined, null]
    13     //接收2个参数
    14     console.log(arr1.slice(1,3));     //输出:(2) ["b", "c"]
    15     //参数为负数。用数组长度加上该数来确定相应的位置
    16     console.log(arr1.slice(-4,-2));     //输出:(2) ["b", "c"]
    17 
    18     //splice()
    19     //删除任意数量的项
    20     //splice(要删除的第一项的位置,要删除的项数)
    21     var removed = arr1.splice(3,2);
    22     console.log(removed);    //输出:(2) [undefined, null]
    23     console.log(arr1);        //输出:(3) ["a", "b", "c"]
    24     //插入任意数量的项
    25     //splice(起始位置,要删除的项数,要插入的项)
    26     var inserted = arr1.splice(1,0,"insert");
    27     console.log(inserted);   //输出:[]
    28     console.log(arr1);         //输出:(4) ["a", "insert", "b", "c"]
    29     var inserted2 = arr1.splice(1,2,"insert2");
    30     console.log(inserted2);   //输出:(2) ["insert", "b"]
    31     console.log(arr1);          //输出:(3) ["a", "insert2", "c"]
    32     //向指定位置插入任意数量的项,同时删除任意数量的项
    33     //splice(起始位置,要删除的项数,要插入的任意数量的项)
    34     var instead = arr1.splice(1,2,"b");
    35     console.log(instead);        //输出:(2) ["insert2", "c"]
    36     console.log(arr1);            //输出:(2) ["a", "b"]
    37 </script>    

    以上为学习《JavaScript 高级程序设计》》(第 3 版) 所做笔记。

  • 相关阅读:
    mysql 用命令行复制表数据到新表
    mysql 表存储位置更改
    mysql 配置延迟复制
    kvm添加网卡
    kvm启动报错Could not access KVM kernel module: Permission denied
    Slave SQL thread retried transaction 10 time(s) in vain, giving up. Consider raising the value of t
    安装tomcat
    mysql 启动报错
    xtrabackup安装及全备,增量备份,及恢复
    mysql gtid 配置
  • 原文地址:https://www.cnblogs.com/xiaoxuStudy/p/12297105.html
Copyright © 2020-2023  润新知