Thursday, 15 March 2012

JavaScript Multiple ways to call a function -



JavaScript Multiple ways to call a function -

write single javascript function calculate + b. function has able called in 2 different ways:

calculate(a, b); calculate(a)(b);

below code came solve naively. please sense free inquire questions why works or done improve it.

var calculate = function () { var arg1 = arguments[0]; if (arguments.length === 2) { homecoming arg1 + arguments[1]; } if (arguments.length === 1) { homecoming function (secondnum) { homecoming secondnum + arg1; } } }

as can see, there few concepts @ work in function. arguments array available within javascript function. allows examine arguments passed function. can see, first check if have 2 arguments, clues me in fact should add together them together.

in sec case, got 1 argument, i'll homecoming function help me accomplish function(x)(y) case.

javascript function

No comments:

Post a Comment