What is the Chrome console displaying with log()?

[亡魂溺海] 提交于 2019-12-12 18:00:48

问题


I think I may have found a bug with Google Chrome (16.0.912.75 m, the latest stable at the time of this writing).

var FakeFancy = function () {};
console.log(new FakeFancy());

var holder = {
    assignTo : function (func) {
        holder.constructor = func;
    }
};

holder.assignTo(function () { this.type = 'anonymous' });
var globalConstructor = function () { this.type = 'global' }

console.log(new holder.constructor());

If you run that block in Firefox, it shows both listed as "Object" with the second having type = local, pretty good. But if you run it in Chrome, it shows

> FakeFancy
> globalConstructor.type

If you expand the trees, the contents are correct. But I can't figure out what Chrome is listing as the first line for each object logged. Since I'm not manipulating the prototypes, these should be plain old objects that aren't inheriting from anywhere.

At first, I thought it was WebKit related, but I tried in the latest Safari for Windows (5.1.2 7534.52.7) and both show up as "Object".

I suspect that it's attempting to do some guesswork about where the constructor was called from. Is the anonymous constructor's indirection messing it up?


回答1:


The first line is a result of

console.log(new FakeFancy());

The WebKit console generally tries to do "constructor name inference" to let you know what type of object it's outputting. My guess is that the more recent version included with Chrome (as opposed to Safari 5.1) can do inference for constructor declarations like

var FakeFancy = function () {};

and not just ones like

function FakeFancy() {}

so that's why you're seeing the disparity.



来源:https://stackoverflow.com/questions/8796806/what-is-the-chrome-console-displaying-with-log

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!