Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Function constructor is eval #211

Closed
thr0w opened this issue Jul 29, 2015 · 7 comments

Comments

@thr0w
Copy link

commented Jul 29, 2015

I'm using Function constructor because need pass parameters to code. But standard not accept it.

What you suggest to do?

var code = 'param.sum = param.a + param.b'
var fn = new Function('param', code)
var res = fn({a: 1, b: 2})
assert(res.sum, 3)
@julien-f

This comment has been minimized.

Copy link

commented Jul 29, 2015

I don't understand your need, why not simply create a regular function?

function fn (param) {
  param.sum = param.a + param.b
  return param
}
var res = fn({a: 1, b: 2})
assert(res.sum, 3)
@thr0w

This comment has been minimized.

Copy link
Author

commented Jul 29, 2015

Because code is generated. I need eval or new Function. I'm choosing for new Function because eval expose all my context.

@julien-f

This comment has been minimized.

Copy link

commented Jul 29, 2015

Standard is based on ESLint and there is a way to ignore some warnings, check in the README.

@thr0w

This comment has been minimized.

Copy link
Author

commented Jul 29, 2015

Ok @julien-f , thanks. I will disable in my code.

But what do you think? If it's necessary parse a generated code why use eval? Eval expose all scope to the evaluated code, it's not safe.

@thr0w thr0w closed this Jul 29, 2015

@julien-f

This comment has been minimized.

Copy link

commented Jul 29, 2015

I am not sure I understand your question :p

It is probably indeed better to use new Function() than eval() but it's still risky and that is why you have to manually disable the warning.

@thr0w

This comment has been minimized.

Copy link
Author

commented Jul 29, 2015

ok. Sorry. It's explained here.
https://jslinterrors.com/the-function-constructor-is-eval

Thank you

@yoshuawuyts

This comment has been minimized.

Copy link
Contributor

commented Jul 29, 2015

@lock lock bot locked as resolved and limited conversation to collaborators May 11, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
3 participants
You can’t perform that action at this time.