Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I add "use strict" to my meteor files?

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.

like image 877
Marco de Jongh Avatar asked May 23 '14 12:05

Marco de Jongh


People also ask

Should you use use strict?

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.

When would you not use strict mode?

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.

Why do you need a strict mode?

Strict mode makes several changes to normal JavaScript semantics: Eliminates some JavaScript silent errors by changing them to throw errors.

How do I get rid of use strict?

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.


1 Answers

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.

like image 73
pfkurtz Avatar answered Sep 23 '22 03:09

pfkurtz