本文介绍了jshint期望函数的新“前缀”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CSiginIn CSignUp CTryIt CBlocks 是所有声明的函数

CSiginIn, CSignUp, CTryIt, CBlocks are all functions declared as such

function CSignIn(){//stuff here}

但JSHint说我错过了'新''前缀'。我该怎么做才能解决这个问题?

yet JSHint says that I am missing the 'new' 'prefix'. What can I do to fix this?

它们只是模块模式中的函数。另外,它要求我删除我在函数结束时放置的分号。

They are just functions inside the module pattern. Also, it is asking me to remove semicolons I had placed at the end of the function which I have done.

var Control = ( function () 
{

    /**
     *Publik
     */

    var publik = function ( page )
    {
        // page 1 initialization

        if( page == 1 )
        {
            CSignIn();
            CSignUp();
            CTryIt();
            CBlocks();
        }

函数示例。 ..

function CTryIt()
{
    // pull elements

    var tryit_button = document.getElementById( 'tryit_button' );

    // initialize access to Model

    tryit_button.addEventListener( "click", function( )
    { 
        new AjaxRequest().invoke( 'ajax_type=ControlTryIt', 
        function( server_response_text ) 
        { 
            new AjaxResponse( server_response_text, 'page_change' ); 
        } ); 

    }, false );
}


推荐答案

如果 newcap ,JSHint希望以大写字母开头的函数是构造函数,因此可以使用 new 关键字调用。

If newcap is enabled, JSHint expects functions starting with a capital letter to be constructors and therefore to be called with the new keyword.

解决方案:禁用此选项或重命名您的功能。

Solution: Either disable this option or rename your functions.

来自:

不这样做不会破坏你在任何浏览器或环境中的代码,但它会有点难以理解out-by读取代码 - 如果该函数应该与 new 一起使用。这很重要,因为当没有使用 new 的函数时,这个会指向到全局对象而不是新对象。

Not doing so won't break your code in any browsers or environments but it will be a bit harder to figure out—by reading the code—if the function was supposed to be used with or without new. And this is important because when the function that was intended to be used with new is used without it, this will point to the global object instead of a new object.

function MyConstructor() {
    console.log(this);
}

new MyConstructor(); // -> [MyConstructor]
MyConstructor();     // -> [DOMWindow]


有关如何更深入地了解有效,请阅读。

For a more in-depth understanding on how this works, read Understanding JavaScript Function Invocation and "this" by Yehuda Katz.

这篇关于jshint期望函数的新“前缀”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 00:23