1

Still a relative newbie. Sorry about that. I'm trying to declare a function as a property of another function. I'm sure I can do this, and that the syntax is correct. But I keep getting "function statment requires an name" as an error; implying it thinks I'm creating an anonymous function.

Here's the code. It's throwing the error on the hide and show parameters. What am I missing?

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}); }

}

Thanks.

4

2 回答 2

2

这不是声明属性,而是声明标签和函数语句(确实需要名称)。

我猜您希望您的其他代码能够执行band.hide()and 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
            });
        }

    };
})();
于 2013-07-24T16:05:18.740 回答
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}); 
    }
  }
}
于 2013-07-24T16:12:00.680 回答