require()和new require()之间有什么区别?

时间:2022-01-11 13:20:32

The core of my question is, what's the difference between

我的问题的核心是,有什么区别

var fs = new require('fs');

and

var fs = require('fs');

Are there any effects or caveats if I were to use new for all modules everywhere?

如果我在所有模块中使用new,那么会有什么影响或警告吗?

While using Webstorm, I noticed that I can get intellisense working only if I use new require('fs'). Before I start using it consistently for a better development experience, I wanted to know a bit more about it.

在使用Webstorm时,我注意到只有在使用new require('fs')时才能使intellisense工作。在我开始一直使用它以获得更好的开发体验之前,我想了解更多相关信息。

3 个解决方案

#1


5  

First of all: Do not confuse new require("...") (which invokes require as a constructor) with new (require("...")) (which invokes the return value of the require call as a constructor). You are doing the first one.

首先:不要将新的require(“...”)(调用require作为构造函数)与new(require(“...”))混淆(它调用require调用的返回值作为构造函数) 。你正在做第一个。

require was not intended to be invoked as a constructor with new. That's not Node-idiomatic, and generally weird. You shouldn't do it, since it reduces the readbility of your code. A future reader might easily mistake it for new (require("...")) (i.e., calling new on a constructor that is being returned by require), since normal Node style does not use new with require.

require不打算作为new的构造函数调用。那不是Node-idiomatic,而且通常很奇怪。您不应该这样做,因为它会降低代码的可读性。未来的读者可能很容易将其误认为是新的(require(“...”))(即,在由require返回的构造函数上调用new),因为正常的Node样式不使用new with require。

Now, let's talk about actual side effects.

现在,我们来谈谈实际的副作用。

new causes a function to run its [[Construct]] internal method, which takes the following actions:

new导致函数运行其[[Construct]]内部方法,该方法采取以下操作:

  • invoke the function with a this set to a newly-created object whose prototype is set to the function's prototype property

    使用this set调用函数,将其原型设置为函数的prototype属性的新创建的对象

  • Return a result:

    返回结果:

    • if the function returns an object, return the result of the function
    • 如果函数返回一个对象,则返回该函数的结果

    • if the function returns anything else, return the newly-created this object
    • 如果函数返回任何其他内容,则返回新创建的此对象

The return value new require will be the same as require for all modules whose exports value is non-primitive (which is true of virtually any module; they typically export a plain object or a function, which is also a kind of object). In the rare case that your module does export a primitive, though, then new will deny you access to that value.

对于导出值非原始的所有模块,返回值new require将与require相同(几乎任何模块都是如此;它们通常导出普通对象或函数,这也是一种对象)。但是,在极少数情况下,您的模块会导出基元,然后new会拒绝您访问该值。

The only other possible difference between require(...) and new require(...) is that the new variant is supplied with a different this value. However, require appears to ignore its this value totally. (Note that Module.prototype.require -- a different function from normal require -- does use its this value, but that's only used when you require submodules from a module, using module.require(...).)

require(...)和new require(...)之间唯一的另一个可能区别是新变量提供了不同的此值。但是,require似乎完全忽略了它的这个值。 (请注意,Module.prototype.require - 与普通需求不同的函数 - 确实使用了它的这个值,但是只有在需要模块的子模块时才使用它,使用module.require(...)。)

#2


1  

The 'new' operator in Node.js is used to create a new instance of something. As you might know in Node.js functions themselves are first class objects. You can create new instance of a function by using 'new' operator that will reside inside a variable.

Node.js中的“new”运算符用于创建某事物的新实例。正如您在Node.js中所知道的那样,函数本身就是第一类对象。您可以使用将驻留在变量中的“new”运算符来创建函数的新实例。

This is used in order to create many functions with the same body but different name so that they have their variables with different values in a program.

这用于创建具有相同主体但名称不同的许多函数,以便它们在程序中具有不同值的变量。

Require, on the other hand, is used to import a module that you have created elsewhere or an existing node modules like you might do by importing libs in other languages. Require in this case returns to the assigned variable the module that you are importing in your application.

另一方面,要求用于导入您在其他地方创建的模块或者您可能通过导入其他语言的库来执行现有节点模块。在这种情况下,需要将您要在应用程序中导入的模块返回到指定的变量。

When you use 'new' with require, it doesn't do anything but imports a new instance of the module that you are importing. This method is not needed, but it will work.

当您对require使用'new'时,它不会执行任何操作,只会导入要导入的模块的新实例。不需要此方法,但它可以工作。

It is suggested to use new only when you want to create several instances of same object and/or want to clear up the older instance by creating newer instance of another object into the same variable.

建议仅在想要创建同一对象的多个实例和/或想要通过在同一变量中创建另一个对象的较新实例来清除旧实例时使用new。

#3


-1  

Think about it.

想一想。

require returns something.

要求退货。

It is the same as doing:

这与做:

var foo = function(bar) {
    this.bar = bar;
};

var f = foo('bar');
console.log(f.bar); //ERROR
//vs:
var y = new foo('bar');
console.log(y.bar); // 'bar'

So all your code does is invoke new with the return from require

所以你的代码所做的就是从require返回调用new

#1


5  

First of all: Do not confuse new require("...") (which invokes require as a constructor) with new (require("...")) (which invokes the return value of the require call as a constructor). You are doing the first one.

首先:不要将新的require(“...”)(调用require作为构造函数)与new(require(“...”))混淆(它调用require调用的返回值作为构造函数) 。你正在做第一个。

require was not intended to be invoked as a constructor with new. That's not Node-idiomatic, and generally weird. You shouldn't do it, since it reduces the readbility of your code. A future reader might easily mistake it for new (require("...")) (i.e., calling new on a constructor that is being returned by require), since normal Node style does not use new with require.

require不打算作为new的构造函数调用。那不是Node-idiomatic,而且通常很奇怪。您不应该这样做,因为它会降低代码的可读性。未来的读者可能很容易将其误认为是新的(require(“...”))(即,在由require返回的构造函数上调用new),因为正常的Node样式不使用new with require。

Now, let's talk about actual side effects.

现在,我们来谈谈实际的副作用。

new causes a function to run its [[Construct]] internal method, which takes the following actions:

new导致函数运行其[[Construct]]内部方法,该方法采取以下操作:

  • invoke the function with a this set to a newly-created object whose prototype is set to the function's prototype property

    使用this set调用函数,将其原型设置为函数的prototype属性的新创建的对象

  • Return a result:

    返回结果:

    • if the function returns an object, return the result of the function
    • 如果函数返回一个对象,则返回该函数的结果

    • if the function returns anything else, return the newly-created this object
    • 如果函数返回任何其他内容,则返回新创建的此对象

The return value new require will be the same as require for all modules whose exports value is non-primitive (which is true of virtually any module; they typically export a plain object or a function, which is also a kind of object). In the rare case that your module does export a primitive, though, then new will deny you access to that value.

对于导出值非原始的所有模块,返回值new require将与require相同(几乎任何模块都是如此;它们通常导出普通对象或函数,这也是一种对象)。但是,在极少数情况下,您的模块会导出基元,然后new会拒绝您访问该值。

The only other possible difference between require(...) and new require(...) is that the new variant is supplied with a different this value. However, require appears to ignore its this value totally. (Note that Module.prototype.require -- a different function from normal require -- does use its this value, but that's only used when you require submodules from a module, using module.require(...).)

require(...)和new require(...)之间唯一的另一个可能区别是新变量提供了不同的此值。但是,require似乎完全忽略了它的这个值。 (请注意,Module.prototype.require - 与普通需求不同的函数 - 确实使用了它的这个值,但是只有在需要模块的子模块时才使用它,使用module.require(...)。)

#2


1  

The 'new' operator in Node.js is used to create a new instance of something. As you might know in Node.js functions themselves are first class objects. You can create new instance of a function by using 'new' operator that will reside inside a variable.

Node.js中的“new”运算符用于创建某事物的新实例。正如您在Node.js中所知道的那样,函数本身就是第一类对象。您可以使用将驻留在变量中的“new”运算符来创建函数的新实例。

This is used in order to create many functions with the same body but different name so that they have their variables with different values in a program.

这用于创建具有相同主体但名称不同的许多函数,以便它们在程序中具有不同值的变量。

Require, on the other hand, is used to import a module that you have created elsewhere or an existing node modules like you might do by importing libs in other languages. Require in this case returns to the assigned variable the module that you are importing in your application.

另一方面,要求用于导入您在其他地方创建的模块或者您可能通过导入其他语言的库来执行现有节点模块。在这种情况下,需要将您要在应用程序中导入的模块返回到指定的变量。

When you use 'new' with require, it doesn't do anything but imports a new instance of the module that you are importing. This method is not needed, but it will work.

当您对require使用'new'时,它不会执行任何操作,只会导入要导入的模块的新实例。不需要此方法,但它可以工作。

It is suggested to use new only when you want to create several instances of same object and/or want to clear up the older instance by creating newer instance of another object into the same variable.

建议仅在想要创建同一对象的多个实例和/或想要通过在同一变量中创建另一个对象的较新实例来清除旧实例时使用new。

#3


-1  

Think about it.

想一想。

require returns something.

要求退货。

It is the same as doing:

这与做:

var foo = function(bar) {
    this.bar = bar;
};

var f = foo('bar');
console.log(f.bar); //ERROR
//vs:
var y = new foo('bar');
console.log(y.bar); // 'bar'

So all your code does is invoke new with the return from require

所以你的代码所做的就是从require返回调用new