LOLWesta, a social media forum


You are not connected. Please login or register

View previous topic View next topic Go down Message [Page 1 of 1]

1
[Discussion] eval() discussion on Fri Apr 10, 2015 5:59 am

Left


Members
Members
I'll open a thread for eval() method discussion.

There's 3.. or more points you must discuss with :

1. Why does eval means evil?
2. Is there any code that uses eval?
3. How to use eval?


In this discussion, we must discuss about eval. Never get Out of Topic.
Thanks.

View user profile

2
Re: [Discussion] eval() discussion on Fri Apr 10, 2015 8:09 am

Tsu


Members
Members
  1. Why does eval means evil?
    For me, it's not evil at all. It's the script developer's fault for using eval with such an unnecessary place to put the eval method to work.

    Code:
    function evalTest() {
      one = new String('Hey there'); /* we didn't use vars      since it would make it a private variable */
      two = eval(one);
    }

    Well, we didn't have a choice but to convert it into a string. Then how to avoid eval(s)? Simply do .toString(); and it might works.

    Code:
    function evalTest() {
     one = new String('Hey there');
     two = one.toString();
    }


But, there's one thing that will make the eval necessary :

Code:
var one = 2;
var code = 'if(one > 1) { alert("Value bigger than one");  }else{ alert("Value less than one"); }';
eval(code);
undefined

But I will still warn you, using eval() might be harmful, but it's useful in one way.
[*]Is there any code that uses eval()?
Syntax validator might uses eval(). There's a lot of code out there that uses eval(), I'll dunno.
[*]How to use eval?
There are a lot of useful links that shows you how to.. but here's something : http://stackoverflow.com/questions/1826859/is-there-ever-a-good-reason-to-use-eval

View user profile

3
Re: [Discussion] eval() discussion on Fri Apr 10, 2015 9:31 am

sugar


Members
Members
answering tsu

1. i did agreed at this point, that's the developer fault to use it in such an unnecessary place. even if they already know the risk of using eval. except if it's necessary.

2. I didn't really agreed.. a developer which knows how to work with JavaScript would never use

2. i disagree. a developer which knows how to work with js would never use eval even if needed. they must be creative enough to know that eval = EVIL and how to avoid using it. so disagreed.

3. never use the eval method.. even thought you need it.. you just have to know how to use it.. not how to insert it into your function / code...

View user profile

4
Re: [Discussion] eval() discussion on Fri Apr 10, 2015 9:38 am

Left


Members
Members
Discussing about eval(), you did need it. But avoid using it when it's completely unnecessary to use at your script; including if you want to set a new if statement, then you might want to set it normally, except if you want it to run after you execute it, not if you want it to run automatically.

This is an example without eval() :

Code:
var age = 17;
if(age > 12 && <18) {
 console.log("You're a highschool student!");
}else{
 console.log("You're a.. blah.");
}

It would run automatically..

But in this script :

Code:
var age = 17;
var stat = "if(age>12&&<18) { console.log('A highschool student.'); }else{ console.log('Blah'); }';

That won't run automatically. Except if you add this line :

Code:
eval(stat);

It might work.

View user profile

Sponsored content


View previous topic View next topic Back to top Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum