Working of javascript inline functions -
i not able grasp how function(match, p1, p2)
working.
what use of match parameter? code breaks if don't write match parameter.
function incrementstring(input) { if (isnan(parseint(input[input.length - 1]))) return input + '1'; return input.replace(/(0*)([0-9]+$)/, function(match, p1, p2) { var = parseint(p2) + 1; return up.tostring().length > p2.length ? p1.slice(0, -1) + : p1 + up; }); }
p.s: new entirely using js development. however, have been working on jsf , java since past few years.
from mdn:
str.replace(regexp|substr, newsubstr|function[, flags])
in case, can see 2 arguments passed replace
, regular expression literal , function expression. that's:
str.replace(regexp, function)
and mdn tells are:
function (replacement)
a function invoked create new substring (to put in place of substring received parameter 1). arguments supplied function described in "specifying function parameter" section below.
and
the arguments function follows:
etc. etc. won't quote entire table.
if leave match
argument out of parameter list, values assigned p1
, p2
first , second argument instead of second , third. won't values need.
it taking code:
function call_with_one_two_three(f) { f(1,2,3); } call_with_one_two_three(function (one, two, three) { alert(two + three); });
and deciding since weren't using one
didn't need it:
function call_with_one_two_three(f) { f(1,2,3); } call_with_one_two_three(function (two, three) { alert(two + three); });
that giving two + three
3
.
in short: the position of arguments matters (and name doesn't).
Comments
Post a Comment