0

Why Javascript Array.sort function doesnt throw an error when incorrect compare function (i.e. function with number of parameter != 2) is passed to it for sorting. As per my understanding the sort function has optional compareFunction function. This function expects 2 parameter.

Example1: compareFunction with 1 parameter

var ar = [1,4,2,3];
console.log(ar.sort(function(a){return 2-a;}));

Output on console

VM239:2 [4, 2, 3, 1]

Example2: compareFunction with 2 parameter

var ar = [1,4,2,3];
console.log(ar.sort(function(a,b,c){return a-b-c;}));

Output on console

VM408:2 [1, 4, 2, 3]
sapan
  • 150
  • 7

5 Answers5

1

You can overload a function with as many parameters you like, since parameters come from the 'arguments arraylike object' of the function. All the extra parameters are just ignored, so your sort function won't throw an error for it. Javascript is rather flexible when it comes to these things. :)

Shilly
  • 8,511
  • 1
  • 18
  • 24
  • What about the case when we pass lesser parameter? how it is invoked and which element is passed at runtime when such function is passed at runtime ? – sapan Aug 12 '15 at 12:14
  • If you don't pass a value for an argument, it defaults to `undefined`. – Quentin Aug 12 '15 at 12:16
  • Look at Bergi's example. The first parameter you pass when calling a function will reference arguments[0], even if you didn't explicitly add the parameter to the `function() {}`. So the sort function will just always pass the first element of the array as the first parameter and the second element as the second parameter, then repeat with the next elements of the array. The only thing it'll care about is if you have a positive/negative number or zero as the return. The calculation behind the return doesn't matter and hence doesn't even have to use the arguments you passed. – Shilly Aug 12 '15 at 12:18
1

How would it know that your compare function is incorrect?

The number of parameters does not imply anything. function(){return arguments[0]-arguments[1]} is still a working and valid compare function.

By calling the function with some test values maybe, or even the values from the array to be sorted? Yes, that could work, but doesn't prove the function to be correct either. Also, keeping track of the expected results during the sorting incurs unreasonable overhead.

So sort just executes its algorithm, expecting the comparisons to be consistent, and when it thinks that the algorithm has come to an end it stops. If compare is not consistent, the sort may fail, run havoc or not terminate - it is permitted by the spec to exhibit "implementation-dependent behaviour". See this answer for more details.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

Because JavaScript doesn't have the concept that exists in other languages such as "Method Overloading", parameters sent to a method are just assigned to an special object array named arguments which you can access directly, or just use a variable for each position of the expected value in the function declaration.

ie:

Having a function such a:

function sum(){
  var t = 0;
  for(var i = 0; i < arguments.length;i++){
    t = t + arguments[i];
  }
  return t;
}

Allow you to call the function like:

sum();
sum(1,2);
sum(1,2,3);
jcgarciam
  • 392
  • 5
  • 12
1

Functions in Javascript ignore (except by the special variable arguments) any additional parameters, and any missing parameters take the value undefined. This means that for a function defined so

function lol(a, b) {
    console.log(a);
    console.log(b);
}

You could also call it like this

lol(1, 2, 3);

and JavaScript would simply ignore the 3rd parameter and not error out. It would output

1  
2

You could call it like this

lol(1);

and JavaScript wouldn't error out. The parameter b would take on the value undefined, and the console would show

1  
undefined

Note that in all cases you can access the number of arguments and the extra arguments using the special variable arguments from within the function


Looking at your example, what's happening is this

var ar = [1,4,2,3];
console.log(ar.sort(function(a){return 2-a;}));

b is missing but the function wouldn't mind.

In

var ar = [1,4,2,3];
console.log(ar.sort(function(a,b,c){return a-b-c;}));

c is undefined always but the function wouldn't mind.


With regard to the effect the return values (2 - a) and NaN respectively have on the actual sorting Bergi's answer explains it (last paragraph)

Community
  • 1
  • 1
potatopeelings
  • 40,709
  • 7
  • 95
  • 119
0

I found this usefull information which give exact description of javascript internal to answer the issue

If there is a different number of arguments than there are parameters, no error is
raised; JavaScript is perfectly fine with this situation and deals with it as follows:
■ If more arguments are supplied than there are parameters, the “excess” arguments
are simply not assigned to parameter names.
For example, let’s say that we have a function declared as
function whatever(a,b,c) { ... }
If we were to call it with whatever(1,2,3,4,5), the arguments, 1, 2, and 3 would
be assigned to a, b, and c, respectively. Arguments 4 and 5 are unassigned to
any parameters.

■ If there are more parameters than there are arguments, the parameters that
have no corresponding argument are set to undefined.
For example, if we were to call the whatever(a,b,c) function with whatever(1),
parameter a would be assigned the value 1, and b and c would be set to undefined.
sapan
  • 150
  • 7