javascript格式化意见:'vs' [英] javascript formatting opinion: ' vs "
问题描述
在查找jquery示例时,我看到作者倾向于使用'或'来封闭选择器,例如。
In looking up jquery examples, I see that authors tend to go with ' or " to enclose, selectors, for example.
如:
$('#tags')。点击...
$('#tags').click ...
或
$(#tags)。点击
$("#tags").click
这是个人风格的东西,还是有理由为什么一个比另一个好?
Is this a personal style thing, or is there a reason why one is better than the other?
在我的简短经验中,我发现'输入更快。另外,建立json参数更容易'因为你可以在字符串中轻松逃脱。
In my brief experience, I find that ' is faster to type. Also, building up json parameters is easier with ' because you can easily escape " in strings.
推荐答案
我主要使用'用于Javascript字符串。推理是绝大多数HTML / XML标记使用用于属性值。有些属性包含Javascript,例如onclick。其中一些javascript包含字符串文字。因此这些字符串文字最好用'分隔。'因此我保持所有Javascript一致并且我可以随处使用。
I use ' for Javascript strings primarily. Reasoning is the the vast majority of HTML/XML markup uses " for attribute values. Some attributes contain Javascript e.g. onclick. Some of that javascript contains string literals. Hence those string literals are best delimited with '. For this reason I keep all Javascript consistent and use ' everywhere I can.
这篇关于javascript格式化意见:'vs'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!