问题
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