te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Fewer lines for switch statement? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Fewer lines for switch statement? - Stack Overflow

programmeradmin3浏览0评论

I wonder if this … 

inputs.keydown(function (e) {
    switch (e.keyCode) {
        case 13:    //Enter
        case 16:    //Shift
        case 17:    //Ctrl
        case 18:    //Alt
        case 19:    //Pause/Break
        case 20:    //Caps Lock
        case 27:    //Escape
        case 35:    //End
        case 36:    //Home
        case 37:    //Left
        case 38:    //Up
        case 39:    //Right
        case 40:    //Down

        // Mac CMD Key
        case 91:    //Safari, Chrome
        case 93:    //Safari, Chrome
        case 224:   //Firefox
        break;
        default:
        $(this).addClass(fill);
        break;
    }
});

… is also possible with fewer lines? I know I could do an if-condition, but I wonder if I missed something like case 13 && 16 && …

Maybe some of you know a better practice to check all the cases and write fewer lines of code. I'm just wondering.

Thank you in advance!

I wonder if this … 

inputs.keydown(function (e) {
    switch (e.keyCode) {
        case 13:    //Enter
        case 16:    //Shift
        case 17:    //Ctrl
        case 18:    //Alt
        case 19:    //Pause/Break
        case 20:    //Caps Lock
        case 27:    //Escape
        case 35:    //End
        case 36:    //Home
        case 37:    //Left
        case 38:    //Up
        case 39:    //Right
        case 40:    //Down

        // Mac CMD Key
        case 91:    //Safari, Chrome
        case 93:    //Safari, Chrome
        case 224:   //Firefox
        break;
        default:
        $(this).addClass(fill);
        break;
    }
});

… is also possible with fewer lines? I know I could do an if-condition, but I wonder if I missed something like case 13 && 16 && …

Maybe some of you know a better practice to check all the cases and write fewer lines of code. I'm just wondering.

Thank you in advance!

Share Improve this question asked May 12, 2012 at 8:01 mattmatt 44.3k107 gold badges268 silver badges402 bronze badges 1
  • 1 I personally think its better than a series of ORs and ANDs using ifs (which would be longer) – Joseph Commented May 12, 2012 at 8:04
Add a ment  | 

4 Answers 4

Reset to default 7

Just put the codes into an array, and then you can simply check if the value is in the array. Since you are using jQuery, you already have an inArray() method to do this.

var keycodes = [13, 16, 17, 18, 19]; //and so on

//if the keycode is not found in the array, the result will be -1
if ($.inArray(e.keyCode, keycodes) === -1) { 
    $(this).addClass(fill);
}

You could create a "map" of the keys you don't want to handle - map lookups should be somewhere between O(1) and O(log n), depending on the implementation.

var specialKeys = {
    13: 1, // Enter
    16: 1, // Shift
    ...
    224: 1 // Cmd/FF
};

inputs.keydown(function (e) {
    if (e.keyCode in specialKeys) return;
    $(this).addClass(fill);
});

Alternatively as your "keys" are all integers you could fill an array where the indexes are the key code values.

EDIT removed the strings, as suggested by @bažmegakapa

What you have is fine. It's clear, and it'll get handled fairly efficiently. Alternately, you can do it with Array#indexOf:

var list1 = [
        13,    //Enter
        16,    //Shift
        17,    //Ctrl
        18,    //Alt
        19,    //Pause/Break
        20,    //Caps Lock
        27,    //Escape
        35,    //End
        36,    //Home
        37,    //Left
        38,    //Up
        39,    //Right
        40     //Down
];
var list2 = [
        91,    //Safari, Chrome
        93,    //Safari, Chrome
        224    //Firefox
];
inputs.keydown(function (e) {
    if (list1.indexOf(e.keyCode) !== -1) {
        // ...
    }
    else if (list2.indexOf(e.keyCode) !== -1) {
        // ...
    }
    else {
        $(this).addClass(fill);
    }
});

But it's only fewer "lines" of code if you lose the ments, and the ments seem (to me) to be important.

Note that some quite old browsers won't have Array#indexOf and so you may have to shim it (which is easy enough to do). Or use jQuery.inArray instead, since you're using jQuery.

IMHO it's more clear to use the code you pointed out, but for your convenience you can use the conditions inside the case...

case (condition):
//code
break;

Personally I would put all the possible matches into an array and use in_array() from phpjs to check if the value to be tested is there.

发布评论

评论列表(0)

  1. 暂无评论