zoukankan      html  css  js  c++  java
  • ie9下对象for..in..的bug

    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);
          }
        }
    };
    
  • 相关阅读:
    Python语法解析器PLY——lex and yacc in Python
    spider-lang :爬虫语言,专为网络爬虫设计
    使用ANTLR做一个简单的Python SQL语法解析器
    使用Antlr实现简单的DSL
    Wrights Notes
    20个人团建能干些什么?
    zz
    贾跃亭反思乐视节奏过快_公司频道_财新网
    西湖人才网 职称考评
    安能物流
  • 原文地址:https://www.cnblogs.com/CoinXu/p/4675483.html
Copyright © 2011-2022 走看看