search

Home  >  Q&A  >  body text

javascript - 关于一个js函数的问题,为什么已经确定数组为空时,还要再把数组清为空?

函数代码如下:

function getEncodeCache(exclude) {
  var i, ch, cache = encodeCache[exclude];
  if (cache) { return cache; }// <--

  cache = encodeCache[exclude] = [];// <--

  for (i = 0; i < 128; i++) {
    ch = String.fromCharCode(i);

    if (/^[0-9a-z]$/i.test(ch)) {
      // always allow unencoded alphanumeric characters
      cache.push(ch);
    } else {
      cache.push('%' + ('0' + i.toString(16).toUpperCase()).slice(-2));
    }
  }

  for (i = 0; i < exclude.length; i++) {
    cache[exclude.charCodeAt(i)] = exclude[i];
  }

  return cache;
}
PHPzPHPz2902 days ago465

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-04-10 15:02:59

    他是先看看这个cache变量是不是已经定义或者是有没有值了,如果有的话就直接返回,如果没有定义或者没有值,那么就赋值[]

    reply
    0
  • Cancelreply