简体   繁体   中英

How to prevent “missing use strict” at end of IIFE assigned to variable?

Using jshint, if you have something like:

var Thing = (function(){
    "use strict";
    // code and stuff...
}());

I get a "missing use strict" error on the last line. I suppose this is because the var Thing = falls outside of the strict scope. Is there a way to prevent this warning without turning off the use strict warnings entirely?

You can use one more wrapper:

(function(namespace) {
   'use strict';

   namespace.Thing = (function() {
     // code here
   })();

})(window);

In your .jshintrc, remove globalstrict: true and use strict: true http://jshint.com/docs/options/#strict

globalstrict is for file scope 'use strict' and strict is for fonction scope (the one you want).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM