File load order in Meteor

泄露秘密 提交于 2020-01-03 05:19:11

问题


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

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.


回答1:


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.

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) {
  ...
};

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



来源:https://stackoverflow.com/questions/31427322/file-load-order-in-meteor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!