0

I have been learning JavaScript on my own and i came across the use of function keyword function in JavaScript. To be more specific here three different examples of its use. Especially new function in the second example confuses me why new and why not capital F

var sum = new Function('a', 'b', 'return a + b');

console.log(sum(2, 6));
// expected output: 8


----------------------------------------------------------------

var apple = new function () {
    this.type = "macintosh";
    this.color = "red";
    this.getInfo = function () {
        return this.color + ' ' + this.type + ' apple';
    };
}


apple.color = "reddish";
console.log(apple.getInfo());
// expected output: "reddish macintosh apple"
--------------------------------------------------------------------


var getRectArea = function(width, height) {
    return width * height;
}

console.log(getRectArea(3,4));
// expected output: 12



-------------------------------------------------------------------------- 
Junior
  • 9
  • 2

2 Answers2

1

new Function creates a new function using the function constructor (the function called 'Function' with a capitalized 'F').

The confusion comes from the fact that in javascript, constructors are defined just like any other functions (they actually are normal functions).

const o = new function(){ this.foo = "bar"; }; defines an anonymous function (that happens to assign stuffs to this so we could call it a constructor), and uses it to initialize a new object (just created by the new keyword). It is similar to:

function A(){ /* ... */ };
const apple = new A;  // or `new A()` but the parentheses are facultative if
                      // there is no arguments

The only difference is that in your case, the function A has no name and is used only once, immediately after being created.

Your example above is (almost) similar to:

var apple = {
  type: "macintosh",
  color: "red",
  getInfo () {
    return this.color + ' ' + this.type + ' apple';
  }
}
Quentin Roy
  • 7,677
  • 2
  • 32
  • 50
0

The new keyword is used with a constructor to create an object instance. Constructors ARE functions, and functions are a type of objects.

By convention, constructors are named with a capital letter first. new Function() uses the Function constructor to create a new function object. It's not extremely useful, but it can be used to dynamically create runnable code (which also means it comes with many potential risks).
Just like with any object created with new, the result is an instance of its constructor, in this case Function.

new function() {} simply uses an anonymous function as the constructor. Inside the function body this will be the new object instance being created. This doesn't make a lot of sense since the purpose of instantiating an object is to make it have a recognizable "type" and get properties from a prototype, neither of which is possible with an anonymous function as the constructor.

var getRectArea = function(width, height) { return width * height; } is simply creating a function and assigning it to a variable named getRectArea. The function could be intended to be used as a constructor, but in this case it's just a regular function that returns a value.
The function is still an instance of Function, so in theory it can be seen as a shorthand for using the new Function constructor, but in reality it means that the function is interpreted before runtime so that the Javascript interpreter can syntax check it and optimize it, which would be deferred to runtime if it was written as new Function("width", "height", "return width * height")

Lennholm
  • 7,205
  • 1
  • 21
  • 30