1

How can callback function work with one parameter when it requires 2 (selector and data) to go? Why doesn't it throw an error?

let links = document.querySelectorAll("a");
        links.forEach(function(link){
            link.addEventListener("click",function(e){
                e.preventDefault();
                ajax("get",e.target.href,render)
            })
        })
        function ajax(url,metodo,callback){
            let xhr = new XMLHttpRequest
            xhr.open(metodo,url)
            xhr.addEventListener("load",function(){
                if(xhr.status==200){
                    callback(xhr.response)
                }
            })
            xhr.send()
        }
        function render(selector,data){
            document.querySelector(selector).innerHTML = data
        }
esteban druetta
  • 75
  • 1
  • 1
  • 10
  • 1
    It doesn't *require* a `data` parameter - if it's empty, the `innerHTML` will just become `undefined` – CertainPerformance Aug 01 '18 at 02:21
  • Possible duplicate of [How come I can call a function with the wrong number of arguments?](https://stackoverflow.com/questions/15323881/how-come-i-can-call-a-function-with-an-argument-when-the-function-is-defined-wi) – Raymond Chen Aug 01 '18 at 02:27

1 Answers1

1

In javascript, it is not necessary to call with same number of parameters as defined in function definition. If we do not define a default parameter value in function definition, then parameter becomes type of undefined.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters

Default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions

Starting with ECMAScript 2015, there are two new kinds of parameters: default parameters and rest parameters.

Default parameters: In JavaScript, parameters of functions default to undefined. However, in some situations it might be useful to set a different default value. This is where default parameters can help.

In the past, the general strategy for setting defaults was to test parameter values in the body of the function and assign a value if they are undefined. If in the following example, no value is provided for b in the call, its value would be undefined when evaluating a*b and the call to multiply would have returned NaN. However, this is caught with the second line in this example:

function multiply(a, b) {
  b = typeof b !== 'undefined' ?  b : 1;

  return a * b;
}

multiply(5); // 5

With default parameters, the check in the function body is no longer necessary. Now, you can simply put 1 as the default value for b in the function head:

function multiply(a, b = 1) {
  return a * b;
}

multiply(5); // 5

For more details, see default parameters in the reference.

Rest parameters: The rest parameter syntax allows us to represent an indefinite number of arguments as an array. In the example, we use the rest parameters to collect arguments from the second one to the end. We then multiply them by the first one.

 function multiply(multiplier, ...theArgs) {
  return theArgs.map(x => multiplier * x);
}

var arr = multiply(2, 1, 2, 3);
console.log(arr); // [2, 4, 6]

Argument Object:

Using the arguments object, you can call a function with more arguments than it is formally declared to accept. This is often useful if you don't know in advance how many arguments will be passed to the function. You can use arguments.length to determine the number of arguments actually passed to the function, and then access each argument using the arguments object.

For example, consider a function that concatenates several strings. The only formal argument for the function is a string that specifies the characters that separate the items to concatenate. The function is defined as follows:

 function myConcat(separator) {
   var result = ''; // initialize list
   var i;
   // iterate through arguments
   for (i = 1; i < arguments.length; i++) {
      result += arguments[i] + separator;
   }
   return result;
}

You can pass any number of arguments to this function, and it concatenates each argument into a string "list":

// returns "red, orange, blue, "
myConcat(', ', 'red', 'orange', 'blue');

// returns "elephant; giraffe; lion; cheetah; "
myConcat('; ', 'elephant', 'giraffe', 'lion', 'cheetah');

// returns "sage. basil. oregano. pepper. parsley. "
myConcat('. ', 'sage', 'basil', 'oregano', 'pepper', 'parsley');

To make it throw error, if same number of argument is not passed in function, typescript can be used.

Komal
  • 1,068
  • 12
  • 23