簡體   English   中英

如何在分配給變量的IIFE末尾防止“嚴格使用”?

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

如果您有類似以下內容,請使用jshint:

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

我在最后一行收到“缺少嚴格使用”錯誤。 我想這是因為var Thing =超出了嚴格范圍。 有沒有辦法在不完全關閉使用嚴格警告的情況下防止此警告?

您可以再使用一個包裝器:

(function(namespace) {
   'use strict';

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

})(window);

在您的.jshintrc中,刪除globalstrict: true並使用strict: true http://jshint.com/docs/options/#strict

globalstrict用於文件作用域“ use strict”, strict用於功能作用域(您想要的那個)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM