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
.
- 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
.
- let numpos tonumber(position). (if position undefined, step produces value nan).
- returnifabrupt(numpos).
- 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
Post a Comment