Useless function in Google Analytics?

断了今生、忘了曾经 提交于 2019-12-13 19:05:26

问题


I was looking through Google Analytics, and I came across this function (pretty-printed for readability):

ha = function(a) {
  var b = [];
  if (Array.prototype.indexOf) return a = b.indexOf(a), "number" ==
    typeof a ? a : -1;
  for (var c = 0; c < b.length; c++)
    if (b[c] === a) return c;
  return -1
},

This looks like an Array.prototype.indexOf polyfill. Trouble is, instead of the var b = this; you'd expect, there is instead var b = [];. I can't find any circumstance in which this would not return -1.

The last time I found something like this in Google Analytics, I had just made a mistake and it was actually functional. But... I really can't see where I'm going wrong with this one.

Does this code do anything? Or does it just unconditionally return -1?


Sandbox for testing:

var ha = function(a) {
  var b = [];
  if (Array.prototype.indexOf) return a = b.indexOf(a), "number" ==
    typeof a ? a : -1;
  for (var c = 0; c < b.length; c++)
    if (b[c] === a) return c;
  return -1
};
<input id="input" value="ha(5);" /><button onclick="var v=document.getElementById('input').value;console.log(v,eval(v));">Run</button>

来源:https://stackoverflow.com/questions/52710559/useless-function-in-google-analytics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!