简体   繁体   English

从外部文件访问module.exports

[英]Access module.exports from external file

I would like to view the contacts of a function from an external file. 我想从外部文件中查看函数的联系人。

MarionetteJS app.js file: MarionetteJS app.js文件:

module.exports = functionToAccess = (function(superClass) {
  extend(functionToAccess, superClass);

  function functionToAccess() {
    this.doSomething = bind(this.doSomething, this);
    return functionToAccess.__super__.constructor.apply(this, arguments);
  }

  functionToAccess.prototype.defaults = {
    someProperty: 'some value',
    anotherProperty: 'another value',
    canAccessThis: false,
    wouldIlikeTo: true
  };

  [...]

  return functionToAccess;

})(Wrapper);

In an external PHP file, I am trying to alert or console.log the contents of anything from the above file, but preferably the functionToAccess function. 在外部PHP文件中,我试图从上面的文件中提醒或者functionToAccess console.log中的任何内容,但最好是functionToAccess函数。

External JS script inside PHP file: PHP文件中的外部JS脚本:

// Using the RequireJS CDN here resolves 'require is undefined'
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.min.js" type="text/javascript"></script>

var testFileLoad = require(['path/to/app'], function() {

});

console.log(testFileLoad);

This returns a localRequire function. 这将返回localRequire函数。 How can I instead return functionToAccess ? 我怎样才能返回functionToAccess

You need to declare a variable in the callback function, which is where you will have access to your path/to/app code. 您需要在回调函数中声明一个变量,您可以在其中访问您的path/to/app代码。 Try something like this: 尝试这样的事情:

require(['path/to/app'], function(functionToAccess) {
    functionToAccess(); // is available here
});

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

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