2

このコード ブロックを単純化するのを手伝ってください。

同じ機能を維持しながら、最後の2つのifステートメントをどのように組み合わせることができるかについてのアイデア。

ありがとう。

document.addEventListener("keydown", function(e) { // shortcuts
  var mapping = {
    "noctrl9": function() { // tab
      var sStart = textarea.selectionStart,
        text = textarea.value;
      textarea.value = text.substring(0, sStart) + "\t" + text.substring(textarea.selectionEnd);
      textarea.selectionEnd = sStart + 1;
    },
    66: function() { // B
      showHideStatusBar(statusBarOn ? false : true);
    },
    79: openDoc, // O
    82: newDoc, // R
    83: saveDoc, // S
    191: function() { // /
      alert("Welcome to " + appname + "!");
    }
  };
  if (e.ctrlKey && mapping[e.keyCode]) {
    e.preventDefault();
    mapping[e.keyCode]();
  }
  if (mapping["noctrl" + e.keyCode]) {
    e.preventDefault();
    mapping["noctrl" + e.keyCode]();
  }
});
4

1 に答える 1