1

sample function declaration in function :

function Rectangle(height, width) {
  this.height = height;
  this.width = width;
  this.calcArea = function() {
      return this.height * this.width;
  };
  // put our perimeter function here!
  this.calcPerimeter = function() {
    return 2 * this.height + 2 * this.width;
};

sample new function declaration :

var actions = new function() {
    console.log("this is to do something");
}

Why are we using new keyword while declaring a new function but not using new keyword while declaring it in a constructor??

Gaurav Grover
  • 191
  • 1
  • 10

1 Answers1

0

new is an operator. It allocates an object instance (and assigns it a prototype). The functions you create aren't instances of any sort of class, and are not going to be created in multiple copies. The static declaration defines the only instance that will exist.

Paul Kienitz
  • 878
  • 6
  • 25
  • Functions are instances of the `Function` class, and they are created on every function defintion. Not sure what "static declaration" means here. – Bergi Oct 19 '15 at 21:31
  • Functions are not dynamically allocated -- they are created once by declaration. (At least, they are no more dynamically allocated than any `var` is.) You don't go creating a `function calcPerimiter` to perform some task in your program, and then creating 27 different calcPerimiter objects. There's just the one. It occupies a single piece of memory. If you were to somehow make 27 separate copies of it, then the `new` keyword would be appropriate. – Paul Kienitz Oct 19 '15 at 21:37
  • 1
    Every `function` expression that is evaluated instantiates a new Function instance. The *code* for the function probably only exists once, but there is most certainly a new Function object created each time the expression is evaluated. – Pointy Oct 19 '15 at 21:42
  • If a constructor evaluates the expression `this.foo = function () { ... };` multiple times, there is still only one instance of the function object. The expression assigned to the member is a reference which remains the same. The expression does not create a function. It only refers to the single instance, which is static -- a product of compilation, not evaluation. – Paul Kienitz Oct 19 '15 at 21:43
  • Every function is an instance of the Function constructor. – Pointy Oct 19 '15 at 21:55
  • Argh, you're right. But that's kind of an implementation detail. If it worked the way I had said, that would for most purposes be just an optimization, rather than a change of how the language worked. At some point under the covers, each function is essentially implemented once, and is immutable, and because of that there's no appropriate use for the new operator. – Paul Kienitz Oct 19 '15 at 21:56
  • Totally agreed that the `new` operator is doing something wrong in the OP :) And I also agree that though the function object instances are distinct, there's (almost certainly) only one internal "real" instance of the code for the function. – Pointy Oct 19 '15 at 22:19
  • Thank You so much.. I am totally satisfied by your answer.. :) – Gaurav Grover Oct 23 '15 at 08:05