简体   繁体   English

节点-使用在同一文件中导出的功能

[英]Node - Using the function that is exported in the same file

I am trying to use the function that I am exporting in the same file, but I get undefined error: 我正在尝试使用要在同一文件中导出的功能,但出现undefined错误:

 $(document).ready(function(){
     $.get('https://apiEndpoint.com)
        .done(function(data) {
          for (var key in data.subscriptionsInSet) {
            userSubscriptions.push(data.subscriptionsInSet[key].productName);
          }

          myFunction();

      });
  });

module.exports.myFunction = function() {
  console.log(userSubscriptions);
};

How can I use the function that I am exporting, in that same file? 如何在同一文件中使用导出的功能?

You have two simple solutions: 您有两个简单的解决方案:

1) using the complete path when accessing your function: 1)访问函数时使用完整路径:

module.exports.myFunction();

2) declaring your function also as a local variable: 2)还将函数声明为局部变量:

var myFunction = module.exports.myFunction = function(){

When your code grows a little unclear with those declarations, you can also write a purely local code followed by a unique export: 当您的代码对这些声明变得有点不清楚时,您还可以编写纯本地代码,然后进行唯一的导出:

module.exports = {
    myFunction1,
    myFunction2,
};

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

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