简体   繁体   English

Node.js 需要继承吗?

[英]Does Node.js require inheritance?

In my server.js file I included the Underscore.js library.在我的server.js文件中,我包含了Underscore.js库。

var _ = require('underscore')

I have my routes like this:我有这样的路线:

// require routes
require('./routes/document');

In the document route, I want to use Underscore.js.在文档路由中,我想使用 Underscore.js。 But it seems like the _ variable is not inherited/inside the document scope.但似乎_变量不是继承的/在文档范围内。 Does that mean I have to set the _ variable on every single required route?这是否意味着我必须在每条所需的路线上设置_变量? Or is there a more intelligent way to do this?或者有没有更聪明的方法来做到这一点?

Yes, you should set the _ in the files that needs it to be available.是的,您应该在需要它可用的文件中设置 _。

Alternatively, you can put it in the global scope by removing the var part.或者,您可以通过删除var部分将其置于全局范围内。

_ = require('underscore');
require('./routes/document'); // _ will be visible in document as well

Check the Node.js module documentation where require() is thoroughly explained.查看 Node.js 模块文档,其中对 require() 进行了彻底解释。

http://nodejs.org/docs/v0.4.5/api/modules.html http://nodejs.org/docs/v0.4.5/api/modules.html

As for your specifics:至于你的具体情况:

Modules are cached after the first time they are loaded.模块在第一次加载后被缓存。 This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.这意味着(除其他外)每次调用 require('foo') 都会返回完全相同的对象,如果它解析为相同的文件。

Hence, if you require('underscore') in both your parent library and './routes/document', only one instance of the underscore module will be loaded and hence.因此,如果在父库和 './routes/document' 中都需要('underscore'),则只会加载一个下划线模块的实例,因此。 Both variables will be in fact the same object.这两个变量实际上是同一个对象。

And by the way, you don't want to define variables in the global scope as it might generates side effects and potentially overwrite properties in other modules.顺便说一句,您不想在全局范围内定义变量,因为它可能会产生副作用并可能覆盖其他模块中的属性。

Finally, the util module provides an inherits method to subclass another constructor and inherit from its prototypes.最后,util的模块提供了一个继承方法的子类从其原型另一个构造和继承。

http://nodejs.org/docs/v0.4.5/api/util.html#util.inherits http://nodejs.org/docs/v0.4.5/api/util.html#util.inherits

As far as I know Node.js engine "requires/charges" a module/file.js in a different scope (I don't know exactly how), for security reasons (imagine a module could change the variables were it's required. That would be dangerous! More information about this concern: Information hiding ).据我所知,出于安全原因,Node.js 引擎“需要/收费”不同范围内的模块/file.js(我不知道具体如何)(想象一下,模块可以在需要时更改变量。那会很危险!有关此问题的更多信息:信息隐藏)。

The only exception are global Node.js objects that are exposed into the module scope.唯一的例外是暴露在模块范围内的全局 Node.js 对象

A global object is, precisely the object "global", and everything you define without var keyword actually is added to that global object:全局对象正是“全局”对象,您定义的没有 var 关键字的所有内容实际上都添加到该全局对象中:

foo_var = "Whatever"

means that:意思是:

console.log(global.foo_var) // Logs "Whatever"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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