Node.js是否需要继承? [英] Does Node.js require inheritance?

查看:71
本文介绍了Node.js是否需要继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 server.js 文件中,添加了 Underscore.js库.

In my server.js file I included the Underscore.js library.

var _ = require('underscore')

我有这样的路线:

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

在文档路径中,我想使用Underscore.js.但是看来 _ 变量没有被继承/在文档作用域之内.这是否意味着我必须在每个必需的路由上设置 _ 变量?还是有更聪明的方法来做到这一点?

In the document route, I want to use 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.

或者,您可以通过删除 var 部分将其置于全局范围内.

Alternatively, you can put it in the global scope by removing the var part.

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

这篇关于Node.js是否需要继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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