0

As a feature in a sort of game created in HTML5/CSS in combination with javascript, I would like to show some randomly looking code which scrolls in a div.

An easy way to accomplish this would be to actually print the code used to create the game, but I cannot seem to manage to do so.

I have tried to call .toString() on an object I have defined, but it only prints [Object Object], which obviously isn't preferably. The desired effect is basically the same as calling .toString() on a single function, however I would like to do so on the entire code/all objects that I use.

How could I accomplish this?

Edit: Some clarification, I have an object

var Foo = { stuff: [], function1: function() { /* code */ } }

And I would like to be able to use it as follows:

var string = Foo.toString(); $("#myDiv").html(string);

Which will result in myDiv containing the source code of the object called Foo.

Zilarion
  • 302
  • 1
  • 4
  • 15

2 Answers2

0

This is the answer you are looking for:

How to inspect Javascript Objects

You have to navigate the object, there is no function as PHP's var_dump.

Community
  • 1
  • 1
pid
  • 11,472
  • 6
  • 34
  • 63
0

here is something you could use :

var a = {'some':'strange','object':''};
console.log( JSON.stringify(a) );
// outputs : {"some":"strange","object":""}

If your game is targeting IE > 9, then you could further randomize the string with something like (base64 encode/decode):

var a = {'some':'strange','object':''};
console.log( btoa(JSON.stringify(a)) );
// outputs : eyJzb21lIjoic3RyYW5nZSIsIm9iamVjdCI6IiJ9
Goran.it
  • 5,991
  • 2
  • 23
  • 25