【问题标题】:Why can't I iterate over `performance.timing` using Underscore or lodash?为什么我不能使用 Underscore 或 lodash 遍历 `performance.timing`?
【发布时间】:2015-05-27 13:03:28
【问题描述】:

为什么会这样:

setTimeout(function(){
    var myObj = {
        'hello':'world',
        'more':'things'
    };
    
    _.each(myObj, function(value, key){
        console.log(key, value);
    });
    
    // why doesn't this output anything?    
    _.each(performance.timing, function(value, key){
        console.log(key, value);
    });
    
    // just to make sure we can
    console.log(performance.timing);
},500);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

输出这个:

> hello world
> more things
> PerformanceTiming {}

我希望对象的键和值得到与myobj 相同的输出。

下划线:http://jsfiddle.net/a43vb7gd/1/
lodash:http://jsfiddle.net/mkxncwax/1/

在 Ubuntu 14.04.2 LTS 上的 Chrome 43.0.2357.81 和 Firefox 38.0 上重现。

【问题讨论】:

  • 在 Chrome 上运行良好
  • 您在哪个浏览器中遇到问题?
  • 刚刚编辑了您的代码以使用 snipplet,我看到了您在 OSX 上使用 chrome 看到的内容
  • @mshaaban 它在 Ubuntu 14.04 上的 Chrome 和 Firefox 上为我重现。已添加到问题中。

标签: javascript performancecounter


【解决方案1】:

我唯一能想到的就是宿主对象的怪异。 Object.keys(performance.timing) 返回和空数组。如果代码在页面上运行与在控制台中运行,代码的行为也会有所不同。

【讨论】:

    【解决方案2】:

    因为 performance.timing 只是 PerformanceTiming 类的一个实例。这意味着performance.timing.__proto__ 等于PerformanceTiming.prototype

    performance.timing 没有自己的属性,但所有从PerformanceTiming.prototype 继承的属性。您可以通过以下方式获取其所有属性:

    Object.keys(PerfomanceTiming.prototype)
    

    Object.keys(performance.timing.__proto__)
    

    【讨论】:

      猜你喜欢
      • 2015-12-02
      • 1970-01-01
      • 2011-02-02
      • 2012-09-27
      • 1970-01-01
      • 2023-01-05
      • 2018-02-28
      • 1970-01-01
      • 2012-05-16
      相关资源
      最近更新 更多