javascript - Array/String lastIndexOf inconsistent results -


code self describing

[3, 1, 3].lastindexof(3) 2 [3, 1, 3].lastindexof(3, undefined) 0 // wtf?  //ok, lets compare string  '313'.lastindexof(3) 2 '313'.lastindexof(3, undefined) 2 //wow! 

different algorithms in spec.

the array version performs tointeger() on second parameter if 1 provided, substitutes 0 nan.

  1. if argument fromindex passed let n tointeger(fromindex); else let n len-1.

the string version uses tonumber() on second parameter irrespective of whether or not 1 provided, return nan without substitute, algorithm manually substitutes infinity nan.

  1. let numpos tonumber(position). (if position undefined, step produces value nan).
  2. returnifabrupt(numpos).
  3. if numpos nan, let pos +∞; otherwise, let pos tointeger(numpos).

as why, can guess, array version newer of 2 (es5), must have decided nan better off being replaced 0, makes @ least little sense considering falsey evaluation (not it's relevant).


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -