Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array.prototype.del 导致for 异常 #22

Open
RelicOfTesla opened this issue Oct 21, 2015 · 2 comments
Open

Array.prototype.del 导致for 异常 #22

RelicOfTesla opened this issue Oct 21, 2015 · 2 comments

Comments

@RelicOfTesla
Copy link

Array.prototype.del = function(){
console.log('aa');
}
var a = new Array();
a.push(1);
for(var k in a){
console.log(k);
}

输出
0
del

导致数组遍历异常

@will-
Copy link

will- commented Oct 23, 2015

  1. 既然是数组为何用for in遍历
  2. 使用for in要检查hasOwnProperty

@HexagonCarbon
Copy link

HexagonCarbon commented Sep 22, 2017

这是因为定义的del是可枚举的。可以换种方法定义del,而不用修改for in,

Object.defineProperty(
   Array.prototype,"del",{
      set: function (e) {},
      get: function () {
         return function(e)
         {
            e.sort(function (e, t) {return e - t;});
            var t = this.concat([]);
            for (var n = e.length - 1; n >= 0; n--)
               t = t.slice(0, e[n]).concat(t.slice(e[n] + 1));
            return t;
         }
      },
      enumerable: false,
      configurable: false
   }
);

关键是enumerable:false;
用这段代码,直接替换alloyimage.base.js或者alloyimage.js中开头的那个del的定义就可以避免后面使用for in出现问题。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants