Domanda

I have the following code that generates a warning in jshint:

['$inject'] is better written in dot notation

function Config () {
}

/* jshint -W069 */
Config['$inject'] = ['$routeProvider'];

However, the following works as expected (warning silenced):

var Config = {}  
/* jshint -W069 */
Config['$inject'] = ['$routeProvider'];

How can I use a function to build my object and still be able to silence the warning?

Thanks

È stato utile?

Soluzione 2

The best practice in my case was to turn on the following flag for the closure compiler:

angular_pass: true

and use the following annotation:

/**
 * @description App configuration
 * @param {!angular.$routeProvider} $routeProvider
 * @constructor
 * @ngInject
 */

Therefore we do not need to use the following

Config['$inject'] = ['$routeProvider'];

Altri suggerimenti

Just write it in dot notation:

Config.$inject = ['$routeProvider'];

JSHint raises that warning when you're using a valid identifier as the member identifier in a square-bracket-style member expression. There's no reason to do so - the dot notation is shorter.


If that's not an option (as it appears from your comments) you could try wrapping the relevant code in a function. JSHint appears to only obey the directive when it appears in function scope. For example:

(function () {
    function Config() { /* ... */ }
    /* jshint -W069 */
    Config['$inject'] = ['$routeProvider'];
}());
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top