19

Let's have a file.js with this code:

module.exports.func = function(txt) {
    this.a = 1;
    this.b = 2;
    console.log(txt, this);
    return this;
}

Now we have another JS file where we do following:

var r1 = new (require('./file')).func('r1');
var r2 = new require('./file').func('r2');

In r1 case it works as intended - r1 contains reference to the newly created object.

In r2 case it does not work - r2 gets reference to module.exports from within the file.js.

The intention was to create a new object by calling func() constructor. Sure, I can do it also this way which is equal to r1:

var r3 = require('./file');
var r4 = new r3.func('r1');

However, I do not understand why r2 does not behave the same way as r1.

How do the extra parenthesis around require('./file') make a difference?

kumarharsh
  • 18,961
  • 8
  • 72
  • 100
Pavel Lobodinský
  • 1,028
  • 1
  • 12
  • 25

1 Answers1

29

These two versions are fundamentally different.

This one:

new (require('./file')).func('r1');

Executes the require, returning the exports of ./file and then calling the new operator on the results .

This one:

var r2 = new require('./file').func('r2');

Invokes require as a constructor.


Let's look at a more isolated and simple example:

new Date() // creates a new date object
new (Date()) // throws a TypeError: string is not a function
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • 1
    Thanks for a great explanation. Do I understand new operator correctly if I say that when compiler finds a function on new's right side, it calls it as a constructor. However, if there is an object (the r1 case, after (require('./file')) evaluation), it goes on and checks if the file('r1') is a function and then invokes it as a constructor. Am I correct? – Pavel Lobodinský Jul 12 '13 at 07:51
  • Thanks for confirmation. It is sad that such info on how NEW operator is being interpreted I was not able to find anywhere :( – Pavel Lobodinský Jul 12 '13 at 13:30
  • 3
    @PavelLobodinský When in doubt there is only one place to check :) [the spec](http://es5.github.io/#x11.2.2) tells us that it calls the [ [[Construct]] internal method](http://es5.github.io/#x13.2.2) which sets the prototype and runs `this` as the new object. When not in the form ` new NewExpression` (or with argument) then we are not in such a case. Wrapped in `()`s this is interpreted as invoking the `[[Call]]` internal method of `require` (in plain English calling it). Then we're left with a `new NewExpression` which evaluates as the new operator :) – Benjamin Gruenbaum Jul 12 '13 at 13:41
  • Thanks @Benjamin. Also, the spec will come in handy. I did not go into it yes, but I guess will need to. – Pavel Lobodinský Jul 12 '13 at 13:46