문제

The two following lines of code should do exactly the same thing. The first one is a bit more verbose, but this should be the only difference. Still, the 2nd example results in an error. Why?

The following works:

var Model = require('./Model');
new Model();

However, the following results in Uncaught Error: Cannot find module './Model'

new require('./Model')();
도움이 되었습니까?

해결책

It has to do with the operator precedence. If you do this, it will work:

new (require("./Model"))();

What was happening is:

(new require("./Model"))()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top