Skip to content
Akin C edited this page Mar 1, 2019 · 8 revisions

Welcome to the Javascript-closures-over-strings- wiki!

This repository is part of a larger project!

◀️ PREVIOUS PROJECT| NEXT PROJECT ▶️


Javascript`s eval allows to represent code with strings. And maybe there could be a situation where the question arouses: "Is it useful to write code as strings?" And when that doubt comes, it is recommended not to represent code as strings. A reason shall follow with an example:

function TypeYourCode(stringCode)
{
	eval(stringCode);
}


var globalArray = [];

function CodeWorks(code)
{
        //Using a global variable as string code
	TypeYourCode("globalArray.push(0);");
	
	//Outputs: 0
	console.log(globalArray[0]);
	
}


function CodeWorksNot(code)
{
	var localArray = [];
	
        //Using a local variable as string code
	TypeYourCode("localArray.push(1);");
	
	//Should never reach this row of code
	console.log(localArray[0]);
	
}


//---WORKS
CodeWorks();

//---Uncaught ReferenceError: localArray is not defined
CodeWorksNot();

The function eval in function TypeYourCode will interpret global variables correctly, but will not if the variable is local like in CodeWorksNot. It seems that strings are not closures.

The solution for this challenge should be avoiding strings as code and using functions if possible.

Content

The user interaction part could look like the content as seen below by starting "index.html" in a web browser and interacting with its features.

ERROR: No image found!

  • "Steps in Pixel" defines in which steps of pixels the height and width of the rectangle/square should be increased
  • "Number of iterations" defines how many times the rectangle/square should be increased
  • "Time in milliseconds" defines periodically every iteration (Except the first one)
  • 🅱️ utton "GO(Ugly)" starts the benchmark with an implemented code which is not recommended
  • 🅱️ utton "GO(Bad)" starts the benchmark with an implemented code which should produce an error
  • 🅱️ utton "GO(Good)" starts the benchmark with an implemented code which should be fine to use

The colored areas are just for a better readability in the wiki and are not part of the content. 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