2014-10-04 149 views
-3
$(".combinations .combination").each(function() { 
    if ($(this).data("id")) { 
     var i = {}; 
     $(this).find(".metric").each(function() { 
     i[$(this).data("name")] = $(this).data("value") 
    }); 
    var n = !0; 
    $.each(i, function (e) { 
     t.hasOwnProperty(e) && t[e] == i[e] || (n = !1) 
    }), (n || 0 == i.length) && (e.id = $(this).data("id"), e.url = $(this).data("url"), e.link = $(this).data("link"), e.name = $(this).data("name"), e.image = $(this).data("image"))} 
    }), 

我是JavaScript新手,試圖通過閱讀有趣的代碼來學習。Javascript代碼邏輯

請幫我理解上面的代碼; n =!0是什麼意思?

這是什麼和它是如何工作的?

t.hasOwnProperty(e) && t[e] == i[e] || (n = !1) 
(n || 0 == i.length) && (e.id = $(this).data("id") 

它們之間的逗號是什麼意思?

回答

0

這是已重新添加了空格的縮小代碼。這並不意味着被人類閱讀,它的意思是儘可能短。正因爲如此,有這樣的奇怪的東西:

!0 === true // 0 evals to false, "not false" = true 

逗號只是一種添加新指令的方法。它們與JavaScript中的分號非常相似(儘管不完全相同)。例如你可以寫x=5, y=10

hasOwnProperty位實際上是:

if(!(t.hasOwnProperty(e) && t[e] == i[e]){ 
    n = false; 
}