0

テキストエリアからテキスト統計を取得します。

どちらが良いでしょうか?

これです?

function getStats() {
  var text = textarea.value,
    stats = {};
  stats.chars = text.length;
  stats.words = text.split(/\S+/g).length - 1;
  stats.lines = text.replace(/[^\n]/g, "").length + 1;
  return stats.lines + " lines, " + stats.words + " words, " + stats.chars + " chars";
}

それともこれ?

function getStats() {
  var text = textarea.value,
    chars = text.length,
    words = text.split(/\S+/g).length - 1,
    lines = text.replace(/[^\n]/g, "").length + 1;
  return lines + " lines, " + words + " words, " + chars + " chars";
}
4

1 に答える 1

2

2つ目。

パフォーマンス上の理由ではありませんが、Javascript オブジェクトが必要ないときに宣言しているだけです。

変数を格納するオブジェクトを作成することは、次のように使用する場合にのみ意味があります。

function getStats() {
  var text = textarea.value,
    stats = {};
  stats.chars = text.length;
  stats.words = text.split(/\S+/g).length - 1;
  stats.lines = text.replace(/[^\n]/g, "").length + 1;
  return stats;
}
于 2013-10-25T12:15:58.737 に答える