javascript node.js 包含类文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19470107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:41:49  来源:igfitidea点击:

node.js include class file

javascriptnode.jsinclude

提问by daniel7558

I got 2 files:

我有2个文件:

start.js

开始.js

    var ConversationModule = require('./src/classes/conversation/Conversation.js');
    ConversationModule.sayhello();

conversation.js

对话.js

    var ConversationModule = new Object();

    ConversationModule.sayhello = function () {
    console.log("hello");
    };

    exports.ConversationModule = ConversationModule();

In start.js I cannot call the sayhello() method. I get following error

在 start.js 中,我无法调用 sayhello() 方法。我收到以下错误

TypeError: object is not a function

I just don't get it why it doesn't work - I'm new to node :)

我只是不明白为什么它不起作用 - 我是节点的新手:)

回答by hexacyanide

You are trying to export ConversationModuleas a function, which it is not. Use this instead:

您正在尝试导出ConversationModule为函数,但事实并非如此。改用这个:

exports.ConversationModule = ConversationModule;

Since you're also assigning the variable as a property of exports, you'd have to call it like this:

由于您还将变量分配为 的属性exports,因此您必须像这样调用它:

var ConversationModule = require('./file').ConversationModule;
ConversationModule.sayhello();

If you don't want to do that, assign the object to module.exports:

如果您不想这样做,请将对象分配给module.exports

module.exports = ConversationModule;

And call it like this:

并这样称呼它:

var ConversationModule = require('./file');
ConversationModule.sayhello();

回答by Myrne Stol

Given that you've named the file conversation.js, you probably intend to define solely the "conversation module" in that particular file. (One file per logical module is a good practice) In that case, it would be cleaner to change your export code, and leave your require code as you had it originally.

鉴于您已将文件命名为 session.js,您可能打算在该特定文件中单独定义“对话模块”。(每个逻辑模块一个文件是一个好习惯)在这种情况下,更改导出代码会更清晰,并保留原始代码。

start.js

开始.js

var ConversationModule = require('./src/classes/conversation/Conversation.js');
    ConversationModule.sayhello();

conversation.js

对话.js

   var ConversationModule = new Object();

   ConversationModule.sayhello = function () {
     console.log("hello");
   };

   module.exports = ConversationModule;

Assigning something to module.exportsmakes this value available when you require the module with require.

分配东西module.exports使得这个值时,可以使用需要与模块require

回答by Anthony Ruffino

conversation.js:

对话.js:

var conversationModule = new Object();

conversationModule.sayhello = function () {
console.log("hello");
};

exports.conversationModule = conversationModule;

start.js:

开始.js:

var conversationModule =  require('./src/classes/conversation/Conversation.js').conversationModule;
conversationModule.sayhello();