一文搞懂entries的使用 bind 实现myBind 封装getElementsByClassName

entries

基本使用

Array.prototype.entries
返回数组的迭代器对象

const arr = [1, 2, 3, 4, 5];
const it = arr.entries();
console.log(it);

在这里插入图片描述

const arr = [1, 2, 3, 4, 5];
const it = arr.entries();
console.log(it.next());
console.log(it.next());
console.log(it.next());
console.log(it.next());
console.log(it.next());
console.log(it.next());

在这里插入图片描述

当我们用for of 遍历arr时 是无法获得对应数组元素的下标的

const arr = [1, 2, 3, 4, 5];
for (let c of arr) {
  console.log(c);
}

在这里插入图片描述
当然使用for in 是可以解决这个问题的

const arr = [1, 2, 3, 4, 5];
for (let c in arr) {
  console.log(c);
}

在这里插入图片描述
但是我们使用entries可以更好的解决

const arr = [1, 2, 3, 4, 5];
const it = arr.entries();
for (let c of it) {
  console.log(c);
}

在这里插入图片描述
此时用数组的解构方法就可以获得数组的值和下标

const arr = [1, 2, 3, 4, 5];
const it = arr.entries();
for (let c of it) {
  const [key, value] = c;
  console.log(key, value);
}

在这里插入图片描述

用在二维数组的排序

定义一个多维数组

var arr = [
	[23,4535,78,77],
	[34,67,89,9,2],
	[1,3,587,4,4,6],
	[33,51],
	[2,7,3]
]
function sorArr(arr) {
  var _it = arr.entries(),
    _doNext = true;
  while (_doNext) {
    var _r = _it.next();
    if (!_r.done) {
      _r.value[1].sort(function (a, b) {
        return a - b;
      });
      _doNext = true;
    } else {
      _doNext = false;
    }
  }
  return arr;
}

console.log(sorArr(arr));

在这里插入图片描述

bind

bind改变this的指向后返回一个新的函数 不执行 实例化时失效 在Function.prototype上
call改变this指向后立即执行

实现myBind

      Function.prototype.myBind = function (context) {
        if (typeof this !== 'function') {
          throw new Error('Only type "function" is to be called by "myBind');
        }
        var _self = this,
          args = Array.prototype.slice.call(arguments, 1),
          tempFn = function () {};
        var fn = function () {
          var newArgs = Array.prototype.slice.call(arguments);
          _self.apply(this instanceof _self ? this : context, args.concat(newArgs));
        };
        tempFn.prototype = this.prototype;
        fn.prototype = new tempFn();
        return fn;
      };

封装getElementsByClassName

      Document.prototype.getElementsByClassName = Element.prototype.getElementsByClassName =
        document.getElementsByClassName ||
        function (className) {
          var allDoms = document.getElementsByTagName('*'),
            allDomsList = allDoms.length,
            allDomsItem,
            finialDoms = [];
          for (var i = 0; i < allDomsList; i++) {
            allDomsItem = allDoms[i];
            var classArr = trimSpace(allDomsItem.className).trim().split(' '),
              classArrLen = classArr.length,
              classArrItem;
            for (var i = 0; i < classArrLen; i++) {
              classArrItem = classArr[i];
              if (classArrItem === className) {
                finialDoms.push(classArrItem);
                break;
              }
            }
          }
          function trimSpace(str) {
            return str.replace(/\s+/g, ' ');
          }
          return finialDoms;
        };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_聪明勇敢有力气

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值