1

我通过 jsLint 运行了一些 jquery,它告诉我应该用空格替换所有选项卡。那正确吗?混合空格和制表符是否重要?

这是代码的一部分,如果有帮助的话(尽管我的理解是stackoverflow将所有制表符转换为空格):

function modal_dialog(share_div)
{
    $("#share_div_" + share_div).dialog({
        autoOpen: false,
        modal: true,
        height: 158,
        width: 400
    });

    $("#share_div_" + share_div).dialog("open");

    $(document).on("click", ".ui-widget-overlay", function() {
        $("#share_div_" + share_div).dialog("close");
    });
}
4

1 回答 1

6

jQuery is just a JavaScript library, so you're really asking whether your JavaScript code should use tabs or spaces.

The answer is it's really up to you. JSLint is a very opinionated program written by a very opinionated author. His opinions are worth listening to, but should not be taken as gospel - many experienced JavaScript developers have different opinions on some issues, even if they agree with him on others.

If you want a more flexible version of JSLint, try JSHint, a fork of JSLint with more options to customize it and less fussy about things that don't really matter.

Spaces and tabs in particular don't matter, as long as you're consistent in your code and indent things in a way that other developers can understand, and your code is easy to maintain. Mixing spaces and tabs is a bad idea; pick one or the other. At the very least, they should never be mixed in a given source file.

That's not to say that there aren't arguments to be made for spaces or for tabs. I prefer tabs myself and have good reasons for using them. Other people use spaces and have good reasons for using them. It just depends on which reasons are more important to each of us.

If you're editing someone else's file, see what indentation style they're using and stick with it. One thing I find very helpful is to use an editor like Komodo which automatically detects the use of tabs or spaces and how much indentation is used if it's spaces, and sets its indentation settings to match each individual source file. That makes it easy to be consistent with a file I'm editing even if it doesn't follow my own personal conventions.

The way you're indenting your code looks fine, and the interesting thing about it is that either tabs or spaces would work equally well in your style. The one thing I would suggest changing is this:

function modal_dialog(share_div)
{
    // ...
}

Elsewhere you put the open curly brace on the same line instead:

$(document).on("click", ".ui-widget-overlay", function() {
    $("#share_div_" + share_div).dialog("close");
});

Like tabs vs. spaces, this is a case where it's best to be consistent. Pick one brace style or the other and use it everywhere.

In many languages, it wouldn't matter which way you write this. But in JavaScript putting the brace on the same line is a clear winner, because of this case:

function foo()
{
    return
    {
        a: 1,
        b: 2
    }
}

This turns out to be a syntax error because of automatic semicolon insertion. You have to put the curly brace on the same line as the return statement here. (Try it and see.) Since you have to do the return that way, you may as well do it that way everywhere:

function foo() {
    return {
        a: 1,
        b: 2
    }
}

Some coding styles do not work well with tabs, such as this way of writing long function calls that don't fit nicely on a single line:

    someFunctionWithLongName(firstParam,
                             secondParam + somethingAdded - subtracted,
                             thirdParam);

If you use tabs, then secondParam and thirdParam are going to line up with firstParam only if the tab settings are exactly right. This is one reason many people prefer spaces, or at least say "tabs for indentation, spaces for alignment". That is, use tabs up to the beginning of the someFunctionWithLongName call, and then spaces for the additional alignment after that for the second and third parameters.

I think this approach is a maintenance headache, and I prefer to just avoid the problem with a different formatting style:

    someFunctionWithLongName(
        firstParam,
        secondParam + somethingAdded - subtractThis,
        thirdParam
    );

Now once again it doesn't matter whether you choose tabs or spaces. The indentation will work out fine either way, and there's no column alignment where you have to worry about combining tabs and spaces.

Interestingly enough, this is also exactly how we indent code blocks:

    if( a ) {
        b();
        c();
    }

To me there's something very appealing in being able to treat {} and () the same regarding indentation. And yes, I also do [] the same way:

    var array = [
        firstElement,
        secondElement,
        thirdElement
    ];

Not:

    var array = [firstElement,
                 secondElement,
                 thirdElement];
于 2013-06-27T05:31:58.827 回答