function - Counting parameters in Javascript -
what docs @ mozilla says:
console.log((function(...args) {}).length); // 0, rest parameter not counted console.log((function(a, b = 1, c) {}).length); // 1, parameters before first 1 // default value counted
so how ever able count parameters in such cases ?
https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/function/length
there 2 separate things going on here parameters defined , arguments passed.
for defined function, can access number of parameters defined in definition of function fn.length
:
function talk(greeting, delay) { // code here } console.log(talk.length); // shows 2 because there 2 defined parameters in // function definition
separately, within function, can see how many arguments passed function given invocation of function using arguments.length
. suppose had function written accept optional callback last argument:
function talk(greeting, delay, callback) { console.log(arguments.length); // shows how many arguments passed } talk("hello", 200); // cause function show 2 arguments passed talk("hello", 200, function() { // show 3 arguments passed console.log("talk done now"); });
Comments
Post a Comment