I like keeping my javascript files as small as possible and using an architectural pattern. This usually means splitting my js files in Services, Controllers, Models, Views, etc.
Meteor automatically loads all js files. However every variable defined in any js file is processed as a local variable for that file. To be able to access them I have to define them like so:
//global
my_global_function = function(){
}
//not global
var my_global_function = function(){
}
//not global
function my_global_function(){
}
Defining a variable/function without the keyword var
or function
is not good practice. What are the possible alternatives?
I like keeping my javascript files as small as possible and using an architectural pattern. This usually means splitting my js files in Services, Controllers, Models, Views, etc.
Meteor automatically loads all js files. However every variable defined in any js file is processed as a local variable for that file. To be able to access them I have to define them like so:
//global
my_global_function = function(){
}
//not global
var my_global_function = function(){
}
//not global
function my_global_function(){
}
Defining a variable/function without the keyword var
or function
is not good practice. What are the possible alternatives?
- As you state, by not including the 'var' keyword is how you declare that a variable should have global visibility. This is simply the syntax as defined, and you do not seem confused about it. So what do you believe is bad practice? Having globals, or simply declaring them this way? Certainly limiting your use of globals is a very good idea, but this is not relevant to syntax. You could look into splitting your code into packages where externally visible variables must be explicitly declared. It's an excellent practice, but still not one I'd remend just to declare globals differently. – JeremyK Commented Nov 16, 2015 at 10:58
-
That's right, you shouldn't declare them without
var
because that's causing confusion, way better would bewindow.variable
orglobal
variable` server side. I don't believe there is another way to share variables between files without some require() function or sth like that – sdooo Commented Nov 16, 2015 at 11:02 -
If this is for
strict mode
pliance consider the approaches here. – JeremyK Commented Nov 16, 2015 at 11:06
2 Answers
Reset to default 3The best option is to use ES2015 modules.
Meteor does not support modules natively yet, but there are packages that bring this support.
For example, universe:modules.
With modules you can import and export some variables/functions/classes/etc:
// module1.import.js
import alertSomething from './module2'
Meteor.startup(() => {
alertSomething();
});
// module2.import.js
export default function alertSomething() {
alert('something');
}
universe:modules
is not the only solution, there are other similar projects. I love this one specially https://github./thereactivestack/kickstart-simple. It replaces Meteor's build system with WebPack's and enables hot-reloading if you use React.
UPDATE:
Meteor does support ES6 modules now
Since you seem very interested in proper architectural design, I would remend looking at Meteor packages. Essentially, you have to declare any globally exposed variable in the package.js
configuration, which is what you want: as little "leakage" as possible. Within a package, you can afford to be a little bit more sloppy, but you can still use var
(and the absence of var
) for more fine-grained control within a package. This can be done within Meteor right now.
You can find most information in the package documentation. The easiest way to get started is to create a package using meteor create --package [package-name]
. This sets up a basic structure to play with. The api.export
function is what controls exposed variables. See doc here.
Additionally, be careful with adding an unnecessary layer on top of the intrinsic Meteor architectural design. Templates are views, server side methods are services, etc. There are only some things that you don't get out of the box, so usually you'll add something like Astronomy or SimpleSchema.
Adding too much of your own architecture is probably going to end with you fighting the Meteor framework itself...