简体   繁体   中英

Why is jsHint warning me: '(var) is defined but never used'?

I'm using CodeKit to run jsHint over my scripts, and minimise them, and I'd like to understand the following error:

    // create a next button for each text input and wire it for nextQ()!
    var next = $('<a class="button next">Next</a>')
        .on( 'click', function() {
            nextQ( $(this) );
        })
        .insertAfter( $questions.find(':text') );

jsHint warns:

next is defined but never used

Given that the element is insertedAfter() on each page load, is this a limitation of jsHint, or am I doing something wrong? My jQuery is pretty basic, and I'd like to learn from the error!

Well, you are defining a callback, which doesn't need to be assigned to a variable; you can simply do:

$('<a class="button next">Next</a>')
    .on( 'click', function() {
        nextQ( $(this) );
    })
.insertAfter( $questions.find(':text') );

So next serves no purpose.

The next var is unnecessary, as .insertAfter() is called on the return value from .on() . Unless you're going to call something on next , you can just do:

$('<a class="button next">Next</a>')
    .on( 'click', function() {
        nextQ( $(this) );
    })
    .insertAfter( $questions.find(':text') );

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