Resolved Question


#1

i am making a javascript plugin that adds functions that makes js easier to use

<!DOCTYPE html>
<html>
  <head>
    <title>EASYSCRIPT EXAMPLES</title>
  </head>
  <body>
  <div id="n"></div>
<script>
//beginning of EASYSCRIPT
var write = function(text, element){
	
  document.getElementById(element).innerHTML= text;
			
      }
			var ifThenElse = function(If, Then, Else){
						
            if(If=true){
				
eval(Then)
		
    }else{
        
eval(Else)

		}
}
//end of EASYSCRIPT
  
write("HI", "n");
ifThenElse(true, "write("hello", "n")");
</script>
  </body>
</html>

when it has run it should've written "hello" using my write function.

did i do something wrong with ifThenElse(); ?

i know my write function works as ive used it before but i really dont know. please help!


#2

Well, first I'd say hello and here is my list of thing you should improve.

  • <div id="n"> => this has little meaning. Try to use something descriptive for id's as it will make your life easier
  • var ifThenElse => probably not the best name for variable. Try to use something descriptive, that is not actually name of loop
  • function(If, Then, Else) => again, try to use something more meaningful. This are bad variable names, and maybe you should stick to one way of writing variables names (you use camel case in ifThenElse, then first letter capital inside this function's arguments)
  • if(If=true) => you don't try to evaluate anything, you just say that If is always true. If you want to evaluate (compare) values, use == or ===
  • using eval() => I'm not sure if this is good practice either. eval() is tool which should not be used lightly. You should probably call your function inside your loop instead of trying to run something using eval()

Try to follow some good practices, this is not worth repairing at this moment.


#4

Read more about eval() here. You can check examples. eval() can be unpredictable if you don't know how to use it and additionaly, I believe that eval() is slower than calling functions (I'm not sure but you can easily check this). You should really set your code so that you can just pass arguments, and call needed functions inside other functions. I would solve it something like this:

// This is just JS pseudocode so you will still have to do majority of work yourself
function ifThenElse(condition, condTrueValue, condFalseValue) {
  var divId = $('#someId');
  if (condition) {
    changeDivValue(someId, condTrueValue);
  } else {
    changeDivValue(someId, condFalseValue);    
  }
}
function changeDivValue(divId, text) {
  // Replace text inside "divId" with "text"
}

ifThenElse(true, 'This condition is true.', 'This condition is false.');

You can make this functions take more parameters so you can change value of div's of your choosing instead of having it always binded to $('#someId'), but this is the idea, to have a function that will take some value, determine is it true or false (or maybe 1, 2 or 3 or some strings values or whatever you want) and then proceed to manipulate with div's values accordingly.


#5

Don't.
 


#6
  1. what is pseudo code
    2 what does the $ mean
    3.this doesn't make any sense. i just wanna know how eval works.

#7

Well that is tough, because this is not a hacker community that shares that sort of stuff with complete strangers. Google it. Yiou stand a better chance than quizzing code beginners.

Code that can be written on paper that makes sense to the reader.

It means we are running a library, jQuery probably.


#8

1 what is pseudo code
2 what does the $ mean
3 this doesn't make any sense. i just wanna know how eval works.

For #3, it's easy. $ is used to indicate we are using, in this case, jQuery library and want to have jQuery functions on object we're using it on.

As for #1, pseudocode, it's as @mtf said, a code that can be written down and it would make sense to person reading it. So essentially, pseudocode is used to describe how some app should be functioning. Pseudocode is actually important because it's good practice to design app before you start writing code. To read more about pseudocode, you can follow this guide, it has good set of rules with examples to help you understand how to write real good pseudocode.

And hardest is of course #2. I am a bit confused by a fact that you said you're making a plugin that adds functions that makes JS easier to use, so like your own library, which is great project and good way to learn, but you should probably first read something about JS and coding styles. This is just a friendly advice if you want to make a living in development. If you want, I can recommend you some books, just message me.
And now to your question - "How eval() works?". Well I gave you a link which, if you opened it, has it explained:

The argument of the eval() function is a string. If the string represents an expression, eval() evaluates the expression. If the argument represents one or more JavaScript statements, eval() evaluates the statements.

So basically that is how eval() works. You give it some expression, and it is evaluated. You want an example? Here it is on my codepen, made just for you. If you don't want to open it, here is code for test:

function test() {
  console.log('This is just a test.');
}

console.log('eval() test incoming...');
eval('test()');

I strongly suggest you read the link about eval() I provided first, because there is enough talk and examples there.

Also, as @mtf said few posts ago, DON'T use eval(), especially if you don't know how it works and how it should be used. You should solve problem you posted in first post using function calls, there is no need for using anything else.


#9

Doesnt eval take in a string and run code if the string is proper code?


#10

Also if you do
'''
Var divchanger = $("divId")

//then

Var divchanger = 5

'''
Will it replace "divId"'s value with the value of 5?


#11

Yes, but it can make a site vulnerable to attack.


#12

Yes it will replace value, and for your previous post, again, as @mtf said, it can make site vulnerable and generally, if you don't have good perception of what you're doing and why. It's generally safer and better option to just use functions since with them you can control input, and you are giving some potentially harmful string to eval. If you really want to use eval, you should probably first clean value you are giving to eval using regex and list of blacklisted words or some other method to eliminate harmful code.


#13

You know its possible to hide variables from use in the console right?


#14

Or do what the Igorcaletacar said


#15

Thanks, will rememeber.