您当前的位置: 首页 >  ar

暂无认证

  • 0浏览

    0关注

    92582博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Array.prototype.mypush

发布时间:2021-04-17 13:35:38 ,浏览量:0

目录
  • 1、概念
  • 2、MDN链接地址
  • 3、代码
    • 3.1、功能函数
    • 3.2、函数执行
1、概念

push()方法将一个或多个元素添加到数组的末尾,并返回该数组的新长度。

2、MDN链接地址

MDN - push

3、代码 3.1、功能函数
Array.prototype.mypush = function() { // 初始化被push的数组长度, // 如果长度不是0, // 给len赋值为this.length; // 否则,赋值为0 let len = this.length ? this.length : (this.length = 0) && 0; // 作用:逐一获取传进来的值 let i = 0; while (i < arguments.length) { // 通过长度赋值给this数组。 // 也就是向数组末尾添加元素。 // 自带返回值 this[len] = arguments[i]; ++i; // 同时this数组的长度也要++; ++len; } // 给this数组的length属性重新赋值 this.length = len; // 返回长度 return this.length; }; 
3.2、函数执行
let arrayData = [3, 'string']; console.log(arrayData); // [3, "string"] arrayData.mypush(1, '字符串', { sname: 'object 对象' }, ['array 数组']); console.log(arrayData); // [3, "string", 1, "字符串", {…}, Array(1)] 
关注
打赏
1653961664
查看更多评论
立即登录/注册

微信扫码登录

0.3639s