javascript - Meteor 包含我的 js 文件的顺序

标签 javascript meteor

我有一个 js 文件:

function library_f() {
}

function some_f() {
  library_f();
}

function another_f() {
  library_f();
}

但代码看起来很难看,我决定将我的 js 文件拆分为三个:

one.js:

function library_f() {
}

两个.js:

function some_f() {
  library_f();
}

three.js:

function another_f() {
  library_f();
}

但是现在我得到了错误

library_f() is not defined

如何设置包含我的 js 文件的顺序?

最佳答案

来自docs :

  • Files in the lib directory at the root of your application are loaded first. Files that match main.* are loaded after everything else.

  • Files in subdirectories are loaded before files in parent directories, so that files in the deepest subdirectory are loaded first (after lib), and files in the root directory are loaded last (other than main.*).

  • Within a directory, files are loaded in alphabetical order by filename.

These rules stack, so that within lib, for example, files are still loaded in alphabetical order; and if there are multiple files named main.js, the ones in subdirectories are loaded earlier.

但看起来这些函数无法访问,因为它们不是全局的。在 meteor 中,每个文件的变量/函数不能被另一个文件访问,除非变量或函数是全局的。

所以你需要这样声明你的函数:

library_f = library_f() {
...
}

以便其他文件可以访问它。变量也是如此:

var x = true; //Not accessible by other files
x = true; //Accessible by other files

var dothis = function () {...} //Not accessible by other files
dothis = function() {..} //Not accessible by other files
function dothis() {..} //Not accessible ny other files

关于javascript - Meteor 包含我的 js 文件的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16166509/

相关文章:

javascript - 如何删除多个 mediaelement.js 播放器?

javascript - OO Javascript - Object.prototype 中的对象实例化

javascript - If 与 while 在特定的 JavaScript 代码中

database - meteor 一次或 "static"发布没有收集跟踪

javascript - 返回溢出内容中的子页面 anchor

javascript - 防止@被输入输入字段?

meteor - 如何使用 Meteor 和 Cordova BLE 插件连接到 BLE 设备

javascript - Twilio 响应对象未定义但没有错误?

node.js - Meteor Node 进程 CPU 使用率接近 100%

javascript - 为什么以管理员身份登录时,管理员电子邮件和meteor.user().email 不相等?