简体   繁体   English

JSHINT严格违反函数表达式与函数声明

[英]JSHINT strict violation function expression vs function declaration

I was having trouble to get around JSHINT complaining about the usage of this keyword as a possible strict violation . 我很难绕开JSHINT抱怨将this关键字的使用作为possible strict violation

Initially I had this simple prototype pattern and I was using function declaration . 最初,我具有这种简单的原型模式,并且使用了function declaration In this case JSHINT throws possible strict violation 在这种情况下,JSHINT抛出possible strict violation

(function (ns) {
    'use strict';

    ns.EventData = function (eventData) {        
        this.id = eventData.id;
        this.name = eventData.name;
    };

    ns.EventData.prototype = (function () {        
        return {
            getDate: getDate            
        };
        // function declaration
        function getDate() {
            return ns.utils.formatDate(this.date);
        };

    }());
}(window.myProject));

However when I switched to function expression everything worked just fine. 但是,当我切换到function expression一切正常。 Can anyone explain why the difference ? 谁能解释为什么有区别?

ns.EventData.prototype = (function () {
    // function expression
    var getDate = function () {
        return ns.utils.formatDate(this.date);
    };

    return {
        getDate: getDate            
    };
}());

The thing is when you are using function declaration you can place it wherever you want. 问题是,当您使用函数声明时,可以将其放置在所需的任何位置。 It doesn't matter if you call function before you declare it, it will be available in memory, and you will not get errors. 在声明函数之前调用函数并不重要,它将在内存中可用,并且不会出错。 That's how JavaScript is interpreted. 这就是解释JavaScript的方式。

That's not the case with the expression. 表达式不是这种情况。 Expression needs to be executed before it's called (it's expression, so it makes sense). 表达式需要在调用之前执行(它是表达式,因此很有意义)。 So for example you can do this: 因此,例如,您可以执行以下操作:

  (function (ns) {
      'use strict';

      test();

      function test() {
          console.log('Test');
      }

  }(window.myProject));

But you can't do this: 但是您不能这样做:

(function (ns) {
    'use strict';

    test();

    var test = function() {
        console.log('Test');
    }

}(window.myProject));

I think that's the only difference between those two. 我认为那是两者之间的唯一区别。 Javascript interpreter will find and move all declarations at the top of the scope (in your case (function (ns) {... ) and that's why JSHint is complaining. Javascript解释器将查找所有声明并将其移到范围的顶部(在您的情况下为(function (ns) {... )),这就是JSHint抱怨的原因。

Conclusion, when JSHint see that you are using this inside function declaration it will warn you because interpreter will maybe move that function to different context and you will maybe have different value for this . 结论,当JSHint看到您正在使用this内部函数声明时,它将发出警告,因为解释器可能会将函数移至不同的上下文,并且您可能this具有不同的价值。

That's just a warning and theoretically it's possible that something similar can happen, but in most of the cases you are pretty much sure about your namespaces and contexts, so just ignore it ;) 这只是一个警告,理论上可能会发生类似的事情,但是在大多数情况下,您对名称空间和上下文非常有把握,因此请忽略它;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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