In javascript development running your code in strict mode by adding:
"use strict";
To the beginning of your functions is a common best practice. However I have yet to see anyone do so in a meteor application.
Does this best practice not apply to Meteor?
Maybe because it sets strict mode on a higher level? I know that node can be run with the --use-strict command line parameter to enforce this. But I do not know of a way to do the same in the client.
The purpose of "use strict" is to indicate that the code should be executed in "strict mode". With strict mode, you can not, for example, use undeclared variables. The numbers in the table specify the first browser version that fully supports the directive. You can use strict mode in all your programs.
If you have such an unrestrictedly typed code, that is used variables without declaring. One variable declared within some function/scope and used from somewhere else(it will be undeclared there) and you can't rewrite/change them, then you should not go for "use strict;" mode because it will break the code.
Strict mode makes several changes to normal JavaScript semantics: Eliminates some JavaScript silent errors by changing them to throw errors.
No, you can't disable strict mode per function. Notice how we can define function outside of strict code and then pass it into the function that's strict. You can do something similar in your example — have an object with "sloppy" functions, then pass that object to that strict immediately invoked function.
There are two options that I know, if you want to 'use strict'
at the top of your Meteor files.
The first is to define a global variable like APP
in one of your top Meteor files (and not in strict
mode), then use it to namespace all of your previously global variables:
// first file
APP = {};
// later file
'use strict';
APP.Stuff = new Mongo.Collection('stuff');
APP.Stuff.find({});
The second method is to create a global alias in that first file:
// first file
G = this;
// second file
'use strict';
G.Stuff = new Mongo.Collection('stuff');
Stuff.find({});
The benefit of the latter is that you don't need to type App.
- every time you want to use your "global" references—they still work as globals, and you only need to use G.
- once—for your declarations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With