Difference between syntax to declare a function

Asked

Viewed 199 times

9

Reading this answer, I realized different forms of call for a function using jQuery.

The first way would be this:

$('#dois').on("click", testar);

function testar() {
  console.log('Teste Dois');
}

And the second is this:

$('#um').on("click", function() {
  console.log('Teste Um');
});

There is a difference between these two forms, in addition to the syntax?

Below is a small example to help understanding.

$('#um').on("click", function() {
  console.log('Teste Um');
});


$('#dois').on("click", testar);

function testar() {
  console.log('Teste Dois');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button id="um">Teste 1</button>

<br/>
<br/>
<button id="dois">Teste 2</button>

  • I think not, because in jquery the second parameter is a call to a function. So do $('#dois').on("click", testar); would be the same as $('#dois').on("click", function(){ //... } );

4 answers

13


The biggest difference I see is in the statements.

In option 1, you used a function, and in option 2, an anonymous function.

I believe the main difference in this case is that, with the stated function, it is then passed by callback, you can repeat it for different events (as many times as you want), without repeating unnecessary code.

Example:

$('#dois').on("click", testar);
$(document).on("click", ".botao-dinamico", testar);

function testar() {
  console.log('Teste Dois');
}

As already mentioned, many people do not know that it is possible to do such an operation in jQuery and end up falling into the following problem:

$('#dois').on("click", function () {
       console.log('Teste Dois');
});

$(document).on("click", ".botao-dinamico", function () {
       console.log('Teste Dois');
});

Note that the second example is a unnecessary complication. So I get my first option.

Of course we have to remember that the example is very simple in this case. You could have greater gains in cases where you needed a more complex function.

For example, its function testar could be using this or some specific parameters (such as event) usually, because in the end it won’t make any difference, since all jQuery needs is a callback.

Bonus

If you just want to consider as a curiosity this information that I will pass on, it is still possible to use an anonymous named function:

$('#um').on("click", function onClick() {
  console.log(onClick);
});

In the specific case, it will not enter the global or local scope of Javascript, but will only be accessible from the anonymous function itself.

  • I thought it would be that way. But I wanted to be sure, since I almost never see anyone using a function, but rather creating one for each event.

  • 2

    +1 for the bonus! :)

10

Javascript has some idiosyncrasies and I may have missed something, but under normal conditions there is no difference in use. Both will execute in the same way. One is anonymous and declared inline in the call and the other is named and previously defined somewhere.

I believe that many people do not use a previously defined function because they do not know how to use it. It’s the same thing that people don’t understand the utility of the variable, the person doesn’t know when to use one or when to use the value inline. I think there is a lack of understanding of the language. At the bottom a named function is like a variable, and an anonymous function is like the value.

But it is also reasonable to infer that in many cases creating an anonymous function there in the same call is the most appropriate. Most of the time you just need that function right there, there’s no reason to define it elsewhere.

I already do:

$('#dois').on("click", function() {
    testar();
});

function testar() {
    console.log('Teste Dois');
}

I put in the Github for future reference.

Then it doesn’t, right? Technically it does, and maybe it has a semantic motive, but it’s almost always a mistake because one doesn’t understand what one is doing.

Use makes no difference to jQuery.

I did not enter the merit of using the function statement as function identifier or as variable. This can be noted in What is the difference between the functions var name = Function() and Function name()?. There is a small difference explained there in the question. This is demonstrated in Guilherme Nascimento’s answer.

  • I believe that many people do not use a previously defined function because they do not know how to use.... Earned my +1 because of that comment.

  • If that part of not knowing how to use were only for JS I would be happy. However, it happens in any language. But, good answer

  • 2

    @Randrade yes, the people don’t know very basic programming things, it’s all on the basis of the cake recipe without even understanding a comma of what is there.

  • How would be the use of a previously defined function ?

  • @Magichat as shown in the question. The first example shows this.

  • I find it funny that it is called "previously defined" if it comes after the call, this reading of the block, which theme offers information about process...

  • @Magichat But it is previously defined, if it was not, how could it be used? The fact that you write out of order does not mean that the code is interpreted like this.

  • It is precisely this information that I seek, on how the order of the interpretation of the code happens, when the parameters are requested and tals, knows some theme that addresses this, I believe it should be the compilers, but just to look at the cover already gives dizziness... If you have a question talking about it, thank you for the link..

  • @Magichat is about compilers, in case the JS compiler has two steps, it compiles the global statements and then compiles the bodies of its members.

  • I was looking for this link, now that you added your answer +1, I think this is the main point, or sej is not something about jQuery, but rather about the language js

Show 5 more comments

7

In practice, for your example, they’re similar, they do the same thing.

The advantage of using $('#dois').on("click", testar); is if there are more event headphones that should also call the function testar, or in case of object-oriented programming where often the method is passed that must be executed, instead of exposing the function on the spot.

Otherwise $('#dois').on("click", testar); uses a named function, or function with name; and the other example uses an anonymous function.

4

Just an addendum to explain about the difference of other functions of any kind against:

function testar() {
    console.log('Teste Dois');
}

Unlike the above, almost all other forms will be anonymous functions, for example, if you do this it will always be an anonymous function:

var foo = function () { /** Algo aqui **/ };

document.getElementById("meuId").onclick = foo;

Even if saved in a variable.

If you try to call it that, you’ll recognize it, due to the order of the statement:

var foo;

document.getElementById("meuId").onclick = foo;

foo = function () {
    alert("Oi");
};
<button id="meuId">Teste</button>

But if you do this will work, even if the function comes later:

document.getElementById("meuId").onclick = foo;

function foo() {
    alert("Oi");
}
<button id="meuId">Teste</button>

  • 1

    I liked having shown how this is "really done". + 1

Browser other questions tagged

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