I've been seeing this pattern in various projects I learn from, sometimes when using jQuery developers wrap their code in this function:
jQuery(function ($) { 'use strict'; /* Application code */ }($)); /* End jQuery */ I was trying to look up the explanation in jQuery docs, but didn't really find much. Could you please explain to me why and when should this be used? And are there better practices / alternatives for something like this.
$(document).ready(function(){}). It is called when the page is loaded so you have access to the DOM.function ($) {}immediately is invoked with$and the result of it is passed tojQuery()as parameter.