-
Notifications
You must be signed in to change notification settings - Fork 0
Home
This repository is part of a larger 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.
The user interaction part should look like the content as seen below by starting "index.html" in a web browser.
-
🅱️ 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.
This knowledge was gained:
- Effective JavaScript "68 Specific Ways to Harness the Power of JavaScript" by David Herman
Sources:
-
Location reload() Method by W3Schools
-
Edit and save a file locally with JS asked by Pascal Boutin answered by guest271314