What is the function of Function in jQuery and what is the right way or time to use?

Asked

Viewed 2,509 times

11

Whenever I go to work with jQuery I just create:

$(function() {

});

Because it was the first way I learned, at first I thought it was a "int main()" pattern in C, but then I came across other forms like:

$( document ).ready(function() {

});

$(function() {

});

$j(function() {

});

function funcao(jQuery) {

}

$( document ).ready(funcao);

What is the right way to use it? Or what is the right time to use it? Why does pure Javascript not need it? What is its function?

  • function () {} is Javascript, in case you want to know the difference of $() and $.ready, I’ll edit the title.

2 answers

8


To be honest, they’re very similar. When you use:

$(function() {

});

is a shorthand for:

$( document ).ready(funcao);

which experienced programmers use. When using:

$j(function() {

});

is actually the same as the first example, except that the alias for jQuery is in $j, maybe because $ is being used in this way:

var $ = minhaLib(); // alias para outra biblioteca qualquer
$j = jQuery.noConflict(); // alias para o jQuery

$(document).on( "ready", manipulador) was deprecated in version 1.8 and removed in 3.0

This is because he would only execute the callback if it was attached before the document was ready. This is due to jQuery having slightly changed the way of handling asynchronous events, by that motive.

Some topics on:

  1. https://api.jquery.com/ready/#entry-longdesc
  2. https://jquery.com/upgrade-guide/3.0/#Breaking-change-on-quot-ready-quot-fn-Removed
  • 1

    It is valid to mention that until version 1.8 the form existed $(document).on("ready", handler), but which became obsolete in this version and was removed from jQuery 3?

  • Valid @Andersoncarloswoss! will edit based on documentation.

  • @Lucascosta What does this code do? $j = jQuery.noConflict();

  • and why pure Javascript does not need?

  • It’s in case it’s being used $ as variable, then not to give conflict is used jQuery.noConflict() @Wictorchaves. I edited the answer with an example.

7

$(function() { ... }); 

is just a shorter way of writing

$(document).ready(function() { ... });

both will be executed when your DOM is ready for use.

Look at this snippet of code:

$( document ).ready(function() {

});

is the same thing as:

function funcao(){ ... }
$(document).ready(funcao);

The difference, is that in the first the function is anonymous, and therefore will exist only there in the $(document).ready, in the second, the function funcao exists globally. Both define an Handler for the event of ready of document

$j(function() {

});

$j here, is probably just an alias for the object jQuery, or $, that someone created.

Functions are the same thing in any language, a block of code that does something. What changes is only the syntax.

The jQuery exists to bring us some tools to facilitate working with Javascript. But everything that is done with jQuery can be done only with pure Javascript.

  • 1

    Why Pure Javascript Doesn’t Need?

  • The $? If it is, it’s because the $ is the alliances of the plugin jQuery, which is a library

Browser other questions tagged

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