简体   繁体   English

如何在WebStorm中从一个JavaScript文件调用内部函数到另一个JavaScript文件? (使用module.exports除外)

[英]How to call inner functions from one JavaScript file to another JavaScript file in WebStorm? (except using module.exports)

I tried using module.exports as well but it's not detecting module.exports in my WebStorm. 我也尝试使用module.exports,但未在WebStorm中检测到module.exports。 So, how to enable module.exports in WebStorm? 那么,如何在WebStorm中启用module.exports? Another thing is I am trying to get function from another js file. 另一件事是我试图从另一个js文件获取功能。 Something like we do inheritance in Java I am expecting here in JavaScript. 就像我们在Java中继承一样,我期望在JavaScript中出现。 below is my code:- locate.js 下面是我的代码:-locate.js

/** * */ / ** * * /

var locate = function()
{

 var a = 10;

 function inner(_block)`
{
console.log( "Hello", _block+" "+a )`;
}
locate.inner = inner;
}

locate();
locate.inner( "World" );
//console.log(locate);
module.export = locate;

delocate.js delocate.js

let a;
a = require('./locate.js');
console.log(a);
a.inner("Universe");` //inner is not visible gives error
 a.inner("Universe"); ^ 

TypeError: a.inner is not a function TypeError:a.inner不是函数

Expected output should be HelloUniverse10 预期输出应为HelloUniverse10

it can be 有可能

var locate = function() {

  var a      = 10;
  var locate = {}

  function inner(_block) {
    console.log('Hello',
      _block + ' ' + a);
  }

  locate.inner = inner;
  return locate;
}


module.exports = locate;

and

const a = require('./locate')();
console.log(a);
a.inner("Universe");

Note that this has absolutely nothing to do with Webstorm; 请注意,这与Webstorm完全无关。 your code is run with Node.js, so it's all about javascript and commonJS modules syntax 您的代码是与Node.js一起运行的,所以这一切都与javascript和commonJS模块的语法有关

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

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