2

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
  • 1,509
  • 2
  • 20
  • 40

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
  • 595
  • 4
  • 17
1

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
  • 2,911
  • 1
  • 18
  • 35