3

Suppose the following code:

var a = new Image(1, 1);
a.onload = function() {
    console.log(this);
};
a.src = 'some.address.com/some/image.jpg';

When this image gets loaded, it will print itself to the console, but the output is an HTML element, like so:

<img width="10" height="10" src="http://some.address.com/some/image.jpg">

When I expect that it would print it as any other JavaScript object, something like:

enter image description here

Is there a way to force it to be print like this?

Bruno Finger
  • 2,105
  • 3
  • 27
  • 47

2 Answers2

2

Try console.dir() instead of console.log(). There's some variation of behavior across browsers. More details at: What's the difference between console.dir and console.log?

Community
  • 1
  • 1
mxs
  • 615
  • 6
  • 17
2

Force it to output an object by putting it in one:

var a = new Image(1, 1);
a.onload = function() {
    console.log({obj: this});
};
a.src = 'some.address.com/some/image.jpg';
jperezov
  • 3,041
  • 1
  • 20
  • 36