2013-07-24 64 views
1

仍然是一個相對的新手。對於那個很抱歉。我試圖將一個函數聲明爲另一個函數的屬性。我確信我可以做到這一點,而且語法是正確的。但我不斷收到「函數聲明需要名稱」作爲錯誤;暗示它認爲我正在創建一個匿名函數。'函數聲明需要一個名字'當聲明函數屬性時

這是代碼。它在隱藏和顯示參數上拋出錯誤。我錯過了什麼?

function band(){ 

var width_offset = { 
    high: "left:-376px", 
    low: "up:-200px" , 
} 

hide : function(width_offset){ 
       if ($(document).width < 768){ 
         $("#band").animate({width_offset.low}, {queue: false, duration: 200}); 
       }else{ 
         $("#band").animate({width_offset.high}, {queue: false, duration: 200}); 
       }; 
      } 

show : function(){ $("#band").animate({left:'0px'}, {queue: false, duration: 200}); } 

}

感謝。

+0

請嘗試格式化您的合作所以它更具可讀性。 – Wex

回答

2

這不是聲明一個屬性,而是聲明一個標籤和一個函數語句(的確需要一個名稱)。

我猜你想你的其他代碼能夠做到band.hide()band.show(),在這種情況下,語法是沿着線:

var band = (function() { 

    var width_offset = { 
     high: "left:-376px", 
     low: "up:-200px", 
    }; 

    return { 
     hide: function(width_offset) { 
      if ($(document).width < 768) { 
       $("#band").animate({ 
        width_offset.low 
       }, { 
        queue: false, 
        duration: 200 
       }); 
      } else { 
       $("#band").animate({ 
        width_offset.high 
       }, { 
        queue: false, 
        duration: 200 
       }); 
      }; 
     }, 

     show: function() { 
      $("#band").animate({ 
       left: '0px' 
      }, { 
       queue: false, 
       duration: 200 
      }); 
     } 

    }; 
})(); 
0

你不能做這樣的事情在JavaScript:

{"up:-200px"} != { up: '-200px' } # Not equivalent! 

相應更改代碼:

function band() { 

    var width_offset = { 
    high: { left: "-376px" }, 
    low: { up: "-200px" } 
    }; 

    return { 
    hide: function(width_offset) { 
     if ($(document).width < 768) { 
     $("#band").animate(width_offset.low, {queue: false, duration: 200}); 
     } else { 
     $("#band").animate(width_offset.high, {queue: false, duration: 200}); 
     } 
    }, 
    show : function() { 
     $("#band").animate({left:'0px'}, {queue: false, duration: 200}); 
    } 
    } 
} 
+0

爲什麼downvote? – Wex

+1

也許是因爲你的回答與其他類似?我沒有看到任何理由。我爲你平平了。 – xr280xr

+0

感謝upvote! – Wex