简体   繁体   English

如何在Javascript中正确调用匿名函数?

[英]How do I correctly call a functions of anonymous function in Javascript?

Here is my code: 这是我的代码:

var A = (function(){
    "use strict";

    function FetchJSON(){
        return someValue;
    }

    var Class = function(){
        // how do I correctly call FetchJSON() from inside this class definition?
    };

    return {
        Class: Class,
        fetchJson: FetchJSON
    };
})()

So basically I'm using JSLint to clean up my code and I'm just calling FetchJSON() from inside the Class object/function definition but JSLint is telling me I need to use the word 'new' before the FetchJSON() call and I'm thinking I don't. 因此,基本上我使用JSLint清理代码,而只是从Class对象/函数定义中调用FetchJSON(),但JSLint告诉我我需要在FetchJSON()调用之前使用单词“ new”,然后我以为不是。 The code works with out the word 'new' just fine but JSLint is telling me it should have it. 该代码可以很好地使用“ new”一词,但是JSLint告诉我应该有它。 What's the deal? 这是怎么回事?

Thanks 谢谢

调用函数fetchJSON而不是FetchJSON,因此JSLint认为它不是构造函数。

按照约定,仅打算用作构造函数的函数(即带有new关键字)应以大写字母开头-有关更多详细信息,请参见此问题

It's because the first letter of FetchJSON is capitalized, causing JSLint to interpret it as a constructor. 这是因为FetchJSON的首字母大写,导致JSLint将其解释为构造函数。 If you wish to keep it capitalized despite the warning, you may. 如果您希望在发出警告的情况下仍将其大写,则可以。

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

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