zoukankan      html  css  js  c++  java
  • JavaScript封装一个函数效果类似内置方法concat()

    JavaScript封装一个函数效果类似内置方法concat()

    首先回忆concat()的作用:

    concat() 方法用于连接两个或多个数组。该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本。

    语法

    arrayObject.concat(arrayX,arrayX,......,arrayX);
    

     

    封装函数参考代码:

     1   function likeConcat() {
     2 
     3       function aPush(arr) {
     4         for (let i = 0; i < arr.length; i++) {
     5           result.push(arr[i]);
     6         }
     7       }
     8 
     9       var len = arguments.length;
    10       var result = [];
    11 
    12       for (let i = 0; i < arguments[0].length; i++) {
    13         result[i] = arguments[0][i];
    14       }
    15       for (let i = 1; i < len; i++) {
    16         aPush(arguments[i]);
    17 
    18       }
    19       return result;
    20 
    21     }
    22 
    23     var a = [1, 2, 3],
    24       b = [4, 5, 6],
    25       c = [7, 8, 9];
    26 
    27     console.log(likeConcat(a, b, c));//[1,2,3,4,5,6,7,8]
  • 相关阅读:
    whereis which type find
    souce and bash 的区别
    systemctl daemon-reload
    linux /etc/profile bashrc bash_profile
    ulimt 和 sysctl
    MySQL 问题总结
    asyncio
    Linux 中 MySQL 操作
    总结一波 Redis 面试题
    os 模块 和 re 模块
  • 原文地址:https://www.cnblogs.com/f6056/p/13321964.html
Copyright © 2011-2022 走看看