2

I am looking for a way for Javascript to read itself in as a String.

For example, take the following code.

var Object = {
    myFunction: function() {
        console.log("FUNCTION!");
    }
}

I would like some way of getting that code as a String, line breaks and all. Exactly as it comes down to the user.

I haven't found any similar questions on my investigations, and I've been looking for a good few hours.

EDIT

Note, I didn't specify the serialisation of an Object. I specified the serialisation of code.

var n = 1;
var x = "hello";
var something = "hi";

How can I get that as a String, as well as the Object declaration?

christopher
  • 26,815
  • 5
  • 55
  • 89
  • First, probably not a good idea to call it "Object", but have you tried `Object.myFunction.toString()`? – Pointy Jan 24 '16 at 18:12
  • 1
    Two things I can think of that might help: 1) you could load the script dynamically and read that in as a string; 2) you can wrap the code in a function and use `toString`. – Whymarrh Jan 24 '16 at 18:13
  • 1
    Rather important question: why? What possible use could you have for that string representation, other than then later on "loading it back in from string", something that we all agreed was such a bad idea, we always tell people to never use `eval`, `new Function`, or `setTimeout` with strings. If this code lives in a file, the obvious answer is "just look at the file" but even then: why? – Mike 'Pomax' Kamermans Jan 24 '16 at 18:13
  • Also if you've got a script stored in a separate file on your server, you can fetch it as plain text with ajax. – Pointy Jan 24 '16 at 18:17
  • The attempt is to see if Javascript can hash itself, and compare with a web service to see if there has been a change in the Javascript code. It's more of a venture "for the sake of it", rather to solve any real problem. – christopher Jan 24 '16 at 18:35

3 Answers3

3

var Object = {
    func1: function() {
        console.log("FUNCTION!");
    },
    func2: function(s) {
        console.log(s);
    },
    func3: function(s) {
        console.log(s);
    },
    str1: 'String 1',
    str2: 'String 2',
    obj1: {
       a:'string a'
    }
}

var $console = $('#console');

for(item in Object){
   if(typeof Object[item] == 'object') {
     
      $console.html( $console.html() +"\r\n"+ 'Type of ' + item+':' + typeof Object[item]);
     
      for(objItem in Object[item]){

            $console.html( $console.html() +"\r\n"+ 'Type of ' + item+':'+ Object[item][objItem] +': '+ typeof Object[item][objItem]);
            $console.html( $console.html() +"\r\n"+  Object[item][objItem].toString() );
        
      }
     
   } else {

       $console.html( $console.html() +"\r\n"+ 'Type of ' + item +': '+ typeof Object[item]);
       $console.html( $console.html() +"\r\n"+  Object[item].toString() );
     
   }

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<pre id="console"></pre>

Seems like you couldn't find this in your investigation.

Community
  • 1
  • 1
Adam Azad
  • 11,171
  • 5
  • 29
  • 70
  • Well this serializes an object.. but not all Javascript code. The Object was just an example. What if I've got a script surrounding the object declaration? – christopher Jan 24 '16 at 18:33
0

If can you wrap all your code inside of a function, then consider Function.prototype.toString.

var foo = function() {
    return 1 + 1;
};

console.log(foo.toString());
xrisk
  • 3,790
  • 22
  • 45
0

If it's an external file just load the file in like you would any other text file you're trying to read into JS.

For a specific function in most cases you can just call .toString() on it to get a string. See jsfiddle here: https://jsfiddle.net/811ho66m/

function myFunc() {

    var what = 'who?';
}

alert(myFunc.toString());
Jimbo Jonny
  • 3,549
  • 1
  • 19
  • 23