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
--------------------------------------------------------------------------