博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ie9下对象for..in..的bug
阅读量:6937 次
发布时间:2019-06-27

本文共 1333 字,大约阅读时间需要 4 分钟。

Underscore的源码中有这样几行

// Keys in IE < 9 that won't be iterated by `for key in ...` and thus missed.  var hasEnumBug = !{toString: null}.propertyIsEnumerable('toString');  var nonEnumerableProps = ['valueOf', 'isPrototypeOf', 'toString',                      'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString'];

ie9中,nonEnumerableProps数组中所包含的属性,不能通过for key in 这样的方式取得。

所以underscore在实现_.keys的时候,判断是否有这样的bug存在。

_.keys = function(obj) {    if (!_.isObject(obj)) return [];    if (nativeKeys) return nativeKeys(obj);    var keys = [];    for (var key in obj) if (_.has(obj, key)) keys.push(key);    // Ahem, IE < 9.    if (hasEnumBug) collectNonEnumProps(obj, keys);    return keys;};

collectNonEnumProps中,将constructornonEnumerableProps全部添加到keys

var collectNonEnumProps = function(obj, keys) {    var nonEnumIdx = nonEnumerableProps.length;    var constructor = obj.constructor;    var proto = _.isFunction(constructor) && constructor.prototype || ObjProto;    // Constructor is a special case.    var prop = 'constructor';    if (_.has(obj, prop) && !_.contains(keys, prop)) keys.push(prop);    while (nonEnumIdx--) {      prop = nonEnumerableProps[nonEnumIdx];      if (prop in obj && obj[prop] !== proto[prop] && !_.contains(keys, prop)) {        keys.push(prop);      }    }};

转载于:https://www.cnblogs.com/CoinXu/p/4675483.html

你可能感兴趣的文章
[ACM_水题] ZOJ 3712 [Hard to Play 300 100 50 最大最小]
查看>>
OGG_GoldenGate数据表定义方式DEFGEN(案例)
查看>>
使用jQuery获取radio/checkbox组的值的代码收集
查看>>
计算机:2014考研大纲全面解析
查看>>
linux下系统启动时,几个配置文件 /etc/profile、~/.bash_profile 等几个文件的执行过程,先后顺序...
查看>>
[Android]对BaseAdapter中ViewHolder编写简化
查看>>
php 5.3新特性
查看>>
微信公共服务平台开发(.Net 的实现)3-------发送文本消息
查看>>
Android Http请求方法汇总
查看>>
[物理学与PDEs]第3章第3节 电导率 $\sigma$ 为无穷时的磁流体力学方程组 3.2 向量场过任一随流体运动的曲面的通量对时间的微式及其应用...
查看>>
C#中MessageBox.Show()方法详解
查看>>
Oracle中 HWM与数据库性能的探讨
查看>>
NGUI图集切割代码
查看>>
Bootstrap模态框(modal)垂直居中
查看>>
iOS中NSBundle的使用
查看>>
jquery图片滚动仿QQ商城带左右按钮控制焦点图片切换滚动
查看>>
CSS3+HTML5特效9 - 简单的时钟
查看>>
webservice调用的四种方式
查看>>
警报级别
查看>>
ffmpeg 从内存中读取数据 .
查看>>