2016-04-29 69 views
0

我想从外部文件查看函数的联系人。从外部文件访问module.exports

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); 

在外部PHP文件,我想提醒或从上述文件CONSOLE.LOG 任何的内容,但最好的functionToAccess功能。

外部JS脚本PHP文件中:

// 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); 

这会返回一个localRequire功能。我怎样才能返回functionToAccess

+0

http://requirejs.org/docs/api.html#jsfiles – OrangeDog

回答

0

您需要在回调函数中声明一个变量,这是您可以访问您的path/to/app代码的位置。尝试这样的:

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