require()和新的require()有什么区别? [英] What is the difference between require() and new require()?

查看:228
本文介绍了require()和新的require()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题的核心是,两者之间有什么区别

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

var fs = new require('fs');

var fs = require('fs');

如果我在所有地方的所有模块上都使用new,是否有任何影响或警告?

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

在使用Webstorm时,我注意到只有使用new require('fs')才能使智能感知工作.在开始持续使用它以获得更好的开发体验之前,我想了解更多有关它的信息.

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.

推荐答案

首先:请勿将new require("...")(将require作为构造函数调用)与new (require("..."))(其中调用require调用的返回值作为构造函数).您正在做第一个.

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不应与new一起用作构造函数来调用.那不是Node惯用语,并且通常很奇怪.您不应该这样做,因为它会降低代码的可读性.将来的读者可能会很容易将其误认为new (require("..."))(即,在由返回 require返回的构造函数上调用new),因为普通的Node样式不将newrequire.

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.

现在,让我们谈谈实际的副作用.

Now, let's talk about actual side effects.

new使函数运行其 内部方法,它会执行以下操作:

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

  • 调用this设置为新创建对象的函数,该对象的原型设置为函数的prototype属性

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

返回结果:

  • 如果函数返回一个对象,则返回函数的结果
  • 如果函数返回其他任何内容,则返回新创建的this对象

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

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.

require(...)new require(...)之间唯一可能的区别是new变体提供了不同的this值.但是,require似乎完全忽略了它的this值. (请注意,Module.prototype.require是与常规require不同的功能-确实会使用其this值,但这仅在使用

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()和新的require()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆