Skip to content
Akin C edited this page May 12, 2018 · 15 revisions

Welcome to the Javascript-output-code-as-string- wiki!

This repository is part of a larger project!

◀️ PREVIOUS PROJECT| NEXT PROJECT ▶️


Javascript´s method toString allows the developer to get the code of a function as a string, like the following example should show:

function myCode()
{
	console.log("Hello World!");
}

myCode.toString();

//Output could look like this without /* and */:
/*
"function myCode()
{
	console.log("Hello World!");
}"
*/

It seems there is no defined standard by ECMAScript for the output result of a function as string with the toString method.

But there are limits of outputing code as string with the toString method. Here follows one example:

/*
    continuation of the first listing (code above)
*/

myCode.bind(null).toString();

//Output could look like this without /* and */:
/*
"function () { [native code] }"
*/

📕 The [native Code] could mean that the method bind has been coded in a different language then javascript, like C or C++. This refers not only to the bind method, it seems.

Because of its limitations such use of toString should be considered carefully.

Content

The user interaction part should look like the content as seen below by starting "index.html" in a web browser.

ERROR: No image found!

  • 🅱️ utton "GET CODE" gets the code from the function changableCode with the method toString found within the file "code.js".

  • 🅱️ utton "SAVE CODE" saves the code found in the textarea into a .js file. For that, it opens a window in which the user should select the file "code.js".

  • 🅱️ utton "ACTIVATE CODE" reloads the page, so the new code could take effect.

  • Into the textarea the user can rewrite the code of the function changableCode.

To use the project just download the files and execute "index.html". Note that all files(folder "wiki_images" excluded) should be placed in the same folder so that the functionality of the code is guaranteed.

Clone this wiki locally