简体   繁体   English

为什么我不能要求构造函数并立即将其与browserify一起使用?

[英]Why can't I require a constructor and immediately use it with browserify?

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' 但是,以下结果为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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM