Questions tagged [function-constructor]

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

References

29 questions
43
votes
7 answers

Legitimate uses of the Function constructor

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…
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
7
votes
1 answer

Enable 'new Function' in a Web Worker with CSP

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…
6
votes
3 answers

Javascript: Add method to all objects in array

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 }, { …
BenjiFB
  • 4,545
  • 10
  • 46
  • 53
5
votes
3 answers

Is there a difference in using a constructor to create an object versus returning an object?

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…
5
votes
1 answer

What's the difference between these functions created by `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…
5
votes
1 answer

How to get context of calling function/object?

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…
jsguff
  • 105
  • 1
  • 5
5
votes
3 answers

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…
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
3
votes
2 answers

console.log() an object does not log the method added via prototype in node js console. Or how to print the prototypes also?

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…
Tinu Jos K
  • 888
  • 1
  • 8
  • 21
3
votes
3 answers

JavaScript constructor function, prototype attach method, and 'this'

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…
lance-p
  • 1,050
  • 1
  • 14
  • 28
3
votes
2 answers

JS Function-constructor re-parsed everytime?

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…
midnite
  • 5,157
  • 7
  • 38
  • 52
3
votes
1 answer

Why some functions in JS have a prototype.constructor prop and others don’t? What is the difference between these functions?

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…
Green
  • 28,742
  • 61
  • 158
  • 247
2
votes
2 answers

declaring function with new keyword and self invoke it in javascript

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…
2
votes
1 answer

If we create an object called 'a' from a function constructor, then why is 'a' not an instance of Function?

function person(first, last, age, eye) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eye; } var myFather = new person("John", "Doe", 50, "blue"); console.log(myFather instanceof person);…
thelearner
  • 1,440
  • 3
  • 27
  • 58
2
votes
2 answers

Javascript object instance property getting set unexpectedly

I have the following code: function Foo() { Foo.prototype.test = 0; } Foo.prototype.incTest = function() { Foo.prototype.test++; }; Foo.prototype.getTest = function(name) { console.log(name +" this: " + this.test + " proto: " +…
moesef
  • 4,641
  • 16
  • 51
  • 68
1
vote
2 answers

new Function() return last value?

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)…
Cobertos
  • 1,953
  • 24
  • 43
1
2