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
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.
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.
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 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()
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 viaFunction
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 returnsundefined
. This is not useful, and it is very confusing. Avoidvoid
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With