Mark Canlas
Mark Canlas

Reputation: 9573

What is the Chrome console displaying with log()?

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?

Upvotes: 2

Views: 250

Answers (1)

Domenic
Domenic

Reputation: 112827

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.

Upvotes: 1

Related Questions