Function parameters not previously defined

Asked

Viewed 118 times

1

I have seen some javascript functions with parameters that are not previously defined, but still work. The doubt arose with the function below. The function myFunction() calls the method pessoas.sort(), which in turn takes an auto function invoked as argument. It has the parameter a and the b. Why does it work even though these haven’t been previously defined? Does the JS engine interpret them by itself? How this can be done at other times and with other parameters?

var pessoas = [
{country:"Brazil", name:"Lucas"},
{country:"EUA", name:"Jhon"},
{country:"Japan", name:"Sushi"}]

function myFunction() {
    pessoas.sort(function(a, b){return a.name - b.name});
    show();
}

function show() {
  document.getElementById("demo").innerHTML =
  pessoas[0].country + " " + pessoas[0].name + "<br>" +
  pessoas[1].county + " " + pessoas[1].name + "<br>" +
  pessoas[2].country + " " + pessoas[2].name;
}
<div id="demo"></div>
<button onclick="myFunction()">Mostrar</button>

  • Although it is a good question, I think you have a wrong view of self invoked function, none of these of your example are, see: http://answall.com/a/13365/14262

  • Uhm, you’re right @Marcelobonifazio. I meant that the function that serves as argument of the people method.Sort() is a self invoked function. Thanks for the correction.

1 answer

1


Having optional parameters is common in several native methods, what is "strange here" is the organization callback itself being optional. This is documented, and if it is not passed this callback says to MDN:

If omitted, the array is ordered according to the Unicode code input of each of the characters, according to the conversion of each element to string.

  • Thank you! However, I lack a doubt. Are there parameter values reserved, and decoded automatically by the JS engine? I’ve seen codes using id values, value etc. How it works and what they are?

  • @Lucastrino what do you mean "parameters reserved"? can you give an example?

  • Parameters defined by the language itself. Parameters whose value is automatically interpreted, no declaration required, by the JS engine itself.

  • Follow a better example. Analyze this function. It has the parameter atrib. It has not been declared before: function muda(atrib){ document.getElementById('style-8').src = atrib; } But when I wear it with an event onChange thus: onChange="muda(value);" it returns the attribute value value for the attribute src of the element with id equal to "style-8. My doubt is this. This parameter atrib is defined in the language itself? There are others, and what are they?

  • @Lucastrino I’m not sure I noticed. But in JS all the names of the function parameter variables can be what we want. In your example atrib could be birta or anything else. Some functions have predetermined parameters, such as callback addEventListener which will receive the event object as a first argument. That said there are some pre-defined "variables": this and arguments. These yes are inserted "magically" by Javascript and associated with the function scope. That’s what you were looking for?

  • Yes, exactly. Thank you very much. Whenever I need to take the value of an attribute of any element, I can simply put the name of the attribute in the function argument as in the above example?

  • @Lucastrino can give an example?

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.