-1

I’m using the gulp-jshint which makes me add ‘use strict’ directive in every file and therefore I can’t use my global object emApp, defined in my app.js file like this:

var emApp = angular.module('emApp');

However, I see that jshint says nothing about angular object. I’d like to know why is that and how to do the same thing with my own emApp object.

zhekaus
  • 3,126
  • 6
  • 23
  • 46
  • 1
    It is not clear what you are asking here. `use strict` does not prevent one from declaring variables and does not prevent the angular Javascript from defining the `angular` variable. It sounds like you may be confused about what `strict` mode does. You can read a summary here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode – jfriend00 Jul 26 '15 at 17:20
  • @jfriend00 you should post that as an answer – Dan Jul 26 '15 at 17:24
  • 1
    @DanPantry - it's hard to post an answer when you don't really understand the question. Yes, I think the OP is confused about strict mode, but I don't really know what they're asking. – jfriend00 Jul 26 '15 at 17:29
  • @DanPantry I've tried to clarify my question. I hope you understand now what I'm asking. – zhekaus Jul 26 '15 at 18:12

2 Answers2

2

With 'use strict' you have to assign either to this.emApp (on top level) or window.emApp (works within functions too), no var is needed.

Though the purpose of every modular approach (and Angular's too) is to minimize the usage of global variables, so doing

/*jshint browser: true */
/*global angular */
'use strict';

var emApp = angular.module('emApp');

in every place where you use the module is a good thing.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • better yet, you could use the commonjs pattern through webpack and export the angular module as a commonjs module. that way you don't have to repeat the name everywhere – Dan Jul 26 '15 at 18:05
  • It doesn't explain why global angular object is ignored by jshint. – zhekaus Jul 26 '15 at 18:10
  • @zhekaus Use `/*global angular */`, I've updated the answer for jshint. – Estus Flask Jul 26 '15 at 18:28
  • I’ve found another answer. To make jshint ignore any global variable you have to add this variable into .jshintrc file like it was done for angular object, into section "globals": ... "globals": { "angular": false, "emApp": false } } – zhekaus Jul 26 '15 at 18:31
0

There are two options to tell JSHint about global variables: 1) If you want to tell about the globals in the particular file, use this directive in the beginning of the file:

/* globals myVariable: false */

2) If you want to declare global for all files in your project, you can add this directive in .jshintrc file, like this:

"globals": {
   "myVariabe": true
}

More about globals with JSHint read here: http://jshint.com/docs/

P.S. angular object mentioned in the question was defined in .jshintrc file

zhekaus
  • 3,126
  • 6
  • 23
  • 46