Use function-constructor to identify questions involving the need to evaluate strings in any ECMAScript dialect while avoiding security risks associated with `eval`, such as script injection
As repeatedly said, it is considered bad practice to use the Function constructor (also see the ECMAScript Language Specification, 5th edition, § 15.3.2.1):
new Function ([arg1[, arg2[, … argN]],] functionBody)
(where all arguments are strings…
I'm having trouble getting new Function to work in a Web Worker. I have an HTML page that spawns a Web Worker. This Web Worker executes code through new Function(str). I'm trying to use this in a packaged Chrome app, which requires a page using…
Imagine the following code:
$.get( "ajax/getColorData.php", function( data ) {
this.colorData = data;
});
now envision that the value of 'data' is:
this.colorData = [
{
colorName: 'Red',
colorIsInRainbow:true
},
{
…
Is there any difference in how these functions operate? The first one is more typically of what I think about when thinking of a constructor.
Example 1: using this to name and set properties. Then using new to create a new Book object.
function…
1.var f = new Function("a", "b", "return a+b")
2.var f2 = Function("a", "b", "return a+b")
f and f2 both are a anonymous function. f(1,2) and f2(1,2) both returns 3. So is there any actual internal difference between the two?
Does Function…
function define(prop, value) {
Object.defineProperty( /* context of caller */ , prop, {value: value});
}
function F() {
define('x', 42);
}
var f = new F();
Is there a way to get context (inline commented in code above) of the calling…
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name
}
var tinu = new Person('Tinu');
console.log(tinu.getName()) //Prints the name 'Tinu' - Expected, means the…
I'm working through CodeAcademy JS excercises and have a question about this example:
//Animal class
function Animal(name) {
this.name = name;
}
//Attach sayName method to Animal class
Animal.prototype.sayName = function() {
console.log("Hi…
In MDN, about Functions and function scope, what does it mean by parsed every time it is evaluated? Can this be observed by codes?
Quote in the section Function constructor vs. function declaration vs. function expression:
Functions defined by…
Every function-constructor in JS has a prototype.constructor property. And it stores the definition of the function:
function Rabbit(value) {
this.jumps: value;
}
alert(Rabbit.prototype.constructor); // alerts exactly the definition of the…
I came across a code that look like this:
const tempFunc = exp => {
return new Function(`return ${exp}`)()
}
first question:
is it self invoking the function and return it?.
what does tempFunc return exactly?
second question:
if we call the…
How do I return the last value from the new Function() constructor? Seems eval() does this by default, as well as vm.Script#runInContext.
I just want to create a REPL in the browser (where I can do something like 2+2; and it returns 4 in the output)…