快速.JS无法识别所需的js文件的功能
Express.JS not recognizing required js file's functions
尽管我已经导入了包含我将使用的函数的JS文件,Node.JS说它是未定义的。
require('./game_core.js');
Users/dasdasd/Developer/optionalassignment/games.js:28
thegame.gamecore = new game_core( thegame );
^
ReferenceError: game_core is not defined
你知道出了什么问题吗?Game_core包括以下功能:
var game_core = function(game_instance){....};
添加到game_core.js末尾:
module.exports = {
game_core : game_core
}
到游戏.js:
var game_core = require('./game_core').game_core(game_istance);
在 Node 中要求模块不会将其内容添加到全局范围。每个模块都包装在自己的作用域中,因此您必须导出公共名称:
// game_core.js
module.exports = function (game_instance){...};
然后在主脚本中保留对导出对象的引用:
var game_core = require('./game_core.js');
...
thegame.gamecore = new game_core( thegame );
您可以在文档中阅读有关它的更多信息:http://nodejs.org/api/modules.html#modules_modules
另一种方法:
if( 'undefined' != typeof global ) {
module.exports = global.game_core = game_core;
}
相关文章:
- JS文件的路径正在消失
- 我的外部js文件无法加载
- 在不破坏未定义函数的情况下,对多个视图使用单个js文件
- 动态加载和卸载js文件
- 如何将变量传递到另一个js文件
- 如何将所有JS文件连接到一个文件夹中
- 从Chrome扩展名中的popup.html文件在background.js文件中运行一个函数
- 如何使用php文件中的GET来获取我在.js文件中声明的变量
- 如何将模板中的标记脚本移动到.js文件中
- 什么's是连接供应商js文件的最佳方式
- 如何在定义js文件后为外部javascript文件设置变量
- 如何在JS文件中获取资源(.resx)字符串
- 将*.js文件的内容放入Object中
- 如何从onclick函数设置全局变量并将其传递给另一个JS文件
- 导致内容安全策略(CSP)冲突错误的本地jquery.js文件
- 如何使用Gmail运行.js文件
- 如何定义一个模块并使用它来分离js文件
- Angular JS文件上传到托管服务器
- 如何引用HTML中节点模块中的js文件
- 如何将JS文件从其他文件夹链接到页面