Meteor中的文件加载顺序 [英] File load order in Meteor

查看:62
本文介绍了Meteor中的文件加载顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 server / methods.js 中定义了一些函数,我在一些方法中使用了这些函数,例如:

I have defined some functions in server/methods.js which I use in some of my methods such as:

function randomIntFromInterval(min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min);
}

如果我想在我的方法中使用这些函数,我必须放置它们在 server / methods.js 中。为什么我不能将函数放在 lib / utils.js 中?我认为首先会调用 lib / 中的文件,因此可以在所有其他文件中访问那里的函数。

If I want to use the functions in my methods, I have to place them in server/methods.js. Why can't I place the functions in lib/utils.js? I thought that files in lib/ would be called first, so functions there would be accessible in all other files.

推荐答案

通过定义你的函数函数randomIntFromInterval(min,max){...} 它的可用性实际上仅限于 lib / utils.js 文件,您的功能将无法从服务器上的任何其他JS文件中获得。

By defining your function like this function randomIntFromInterval(min, max) {...} its availability will actually be limited to the lib/utils.js file and your function won't be available from any other JS files on the server.

您必须申报这样的函数是为了将它放在全局范围内并使其可以从其他JS文件访问:

You have to declare your function like this in order to put it in the global scope and make it accessible from other JS files:

randomIntFromInterval = function (min, max) {
  ...
};

注意缺少 var 关键字还会限制 lib / utils.js 文件的功能可用性。

Note the absence of the var keyword which would also limit the function availability to the lib/utils.js file.

这篇关于Meteor中的文件加载顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆