简体   繁体   中英

Access module.exports from external file

I would like to view the contacts of a function from an external file.

MarionetteJS app.js file:

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.

External JS script inside PHP file:

// 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. How can I instead return functionToAccess ?

You need to declare a variable in the callback function, which is where you will have access to your path/to/app code. Try something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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