Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between void, eval, and the Function constructor in JavaScript?

void(document.body.innerText += 'hi')

eval(document.body.innerText +='\nbye')

Function(document.body.innerText += '\n!!!')

void(Function(function foo(){document.body.innerText += '\n>hi2'; return true}).toString())();

eval(Function(function foo(){document.body.innerText += '\nbye2'; return true}).toString())();

Function(Function(function foo(){document.body.innerText += '\n!!!2'; return true}).toString())();

What's the processing model for executing code within these different statements?

void(alert('hi'))
undefined

eval(alert('hi'))
undefined

Function(alert('hi'))
function anonymous() {
  undefined
}

eval(Function(function foo(){return true}).toString())();
TypeError: undefined is not a function

void(Function(function foo(){return true}).toString())();
TypeError: string is not a function

Function(Function(function foo(){return true}).toString())();
undefined
like image 294
Paul Sweatte Avatar asked Apr 27 '12 01:04

Paul Sweatte


People also ask

What is a function constructor in JavaScript?

A constructor is a special function that creates and initializes an object instance of a class. In JavaScript, a constructor gets called when an object is created using the new keyword. The purpose of a constructor is to create a new object and set values for any existing object properties.

Is function better than eval?

Normally, it is better to use new Function() than eval() to evaluate code: The function parameters provide a clear interface to the evaluated code and you don't need the slightly awkward syntax of indirect eval() in order to ensure that the evaluated code can only access its own and global variables.

What is the eval function in JavaScript?

eval() is a function property of the global object. The argument of the eval() function is a string. It will evaluate the source string as a script body, which means both statements and expressions are allowed. It returns the completion value of the code.

What is JavaScript void function?

What is the void keyword? When a function is void, it means that the function returns nothing. This is similar to functions in JavaScript which return undefined explicitly, like so: function und() { return undefined } und()


1 Answers

In this article the eval and Function constructors are explained:

(…) Global, built-in eval function evaluates code in the scope of a caller.

The code executed from within function created by Function constructor doesn’t really execute in global scope. However, it doesn’t execute in local scope either, which is what probably leads to confusion. Function constructor creates a function whose scope chain consists of nothing but a global scope (preceded with function’s own Activation Object, of course). Any code contained in a function created via Function constructor evaluates in a scope of that function, not in a global scope. However, it’s almost as if code executes globally, since global object is the very next object in the scope chain.

And according to this page, void just returns undefined:

In many languages, void is a type that has no values. In JavaScript, void is an operator that takes an operand and returns undefined. This is not useful, and it is very confusing. Avoid void.

like image 111
Jonathan Avatar answered Nov 12 '22 21:11

Jonathan