Skip to content

正方选课系统重写filter参数错误 #4

@ceilf6

Description

@ceilf6

​我在支持时间过滤的时候发现过滤数组一直被清空,然后我打了一堆调试发现是在 .filter 后时间过滤器数组就被清空了,我一脸懵啊,然后我检查了我的脚本的上下文逻辑一直没找到问题,我就想着会不会不是我的问题,是 filter 的问题?然后我去打印了一下

Array.prototype.filter.toString()

一看...

function(f, g) {
  var e = g || window;
  var b = [];
  for (var d = 0, c = this.length; d < c; ++d) {
    if (!f.call(e, d, this[d], this)) {
      continue;
    }
    b.push(this[d]);
  }
  return b;
}

ESM规定的正确的 callback 参数顺序是 value, index, array 

但是它重写成了 d, this[d], this

...

然后我就用

const safeFilter =
  document.createElement('iframe')
    .contentWindow
    .Array
    .prototype
    .filter;

const result = safeFilter.call(arr, s => s);

套了一下

但是我感觉虽然这个 iframe 没有加载资源只是为了拿个干净、开销相对较小,但是肯定不如直接遍历手筛来的无需资源一些

                const stepFilter = [];
                for (let i = 0; i < stepTrim.length; i++) {
                    try {
                        const v = '' + stepTrim[i];
                        if (v && v.length > 0) stepFilter.push(v);
                    } catch (e) {
                        // 忽略无法处理的项
                    }
                }

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions