在调试其他人编写的javascript时,我遇到了一些我以前从未见过的代码.这是一个示例:
function doSomething() {
//doing something here...
}
function doItNow() {
//other logic...
doSomething && doSomething(); // <=== What's this?
}
Run Code Online (Sandbox Code Playgroud)
函数doItNow()中第二行的目的是检查doSomething是否存在然后调用它?像这样:
function doItNow() {
//other logic...
if (doSomething) {
doSomething();
}
}
Run Code Online (Sandbox Code Playgroud)
JSLint不喜欢它,我宁愿在我的应用程序中没有错误的代码.任何见解?
javascript coding-style idioms short-circuiting logical-operators
我在App_Code目录下的文件中有一个帮助方法.该方法在MVC 3中运行得非常好,但在升级到MVC 4后却没有.它在第一个@if()块失败了......
[编辑]这看起来像是Razor引擎中的解析错误.当我的所有对象及其属性都不为null时,我得到'对象引用没有设置为对象的实例'错误.我想我必须直接去微软.
[编辑]错误消息是旧的"对象引用未设置为对象的实例".这很奇怪,因为条件中的每个对象AND属性都是有效的而不是null.
@helper RenderConnectButton(System.Web.Mvc.HtmlHelper helper, ContactTwitterHandleDto contactTwitterHandle, ContactFacebookAccountDto contactFacebookAccount) {
<ul class="socialMedia inlineList">
@if (contactTwitterHandle != null && contactTwitterHandle.IsValid.HasValue && contactTwitterHandle.IsValid.Value)
{
var tHandle = "@" + contactTwitterHandle.Handle;
<li class="twitter">
<a class="btn" href="http://www.twitter.com/@contactTwitterHandle.Handle" target="_blank">
<i></i>
<span class="label">@tHandle</span>
</a>
@if(contactTwitterHandle.FollowerCount.HasValue)
{
<div class="count" id="c">
<i></i>
<u></u>
<span class="followers" title="@contactTwitterHandle.FollowerCount.Value followers">@FollowerCount(contactTwitterHandle.FollowerCount.Value)</span>
</div>
}
</li>
<script type="text/javascript">
$("#twitter").click(function () {
window.location.href = '@(helper.BuildUrlFromExpression<TenantController>(t => t.LinkTwitterAccount()))';
})
</script>
}
@if (contactFacebookAccount != null && contactFacebookAccount.IsValid.HasValue && contactFacebookAccount.IsValid.Value)
{
<li class="facebook">
<a …Run Code Online (Sandbox Code Playgroud)