如何在子切换语句中使用函数参数?

时间:2013-06-26 20:02:29

标签: javascript function switch-statement

在这种情况下,我正在尝试致电shoppingClickHandler(buyItem),希望它能够运行子switch语句,将feature指定为buyItem

但是我返回了错误Uncaught ReferenceError: buyItem is not defined

我应该只向函数发送字符串吗?

function shoppingClickHandler(feature){
    var msg;
    var itemIdentifier = $(this).parent().attr('class');
    switch(feature) {

        case buyItem: msg = "You bought Item #" + itemIdentifier;
        break;

        case tryItem: msg = "You tried Item #" + itemIdentifier + " on";
        break;

        case suggestItem: msg = "You suggested Item #" + itemIdentifier;
        break;

        case giftItem: msg = "You gifted Item #" + itemIdentifier;
        break;

        case add_to_wishlist: msg = "You added Item #" + itemIdentifier + " to your wishlist";
        break;
    }
    makeEntry(msg);
}


//Buy button test
$('.buy').click(function(){
    shoppingClickHandler(buyItem);
})

//Try button test
$('.try').click(function(){

})

//Suggest button test
$('.suggest').click(function(){

})

//Gifting button test
$('.gift').click(function(){

})

//Add to wisthlist test
$('.add_to_wishlist').click(function(){

})

1 个答案:

答案 0 :(得分:0)

用作字符串

  case 'buyItem':
  //ets...