Not very fluent in JavaScript or jQuery for front-end dev. Let's say I have this code:
$("#foo").click(function (event) {
console.log("single click");
return false;
});
$("#foo").dblclick(function (event) {
console.log("double click");
bar();
return false;
});
function bar() { //do stuff };
But I've also seen it wrapped within a function:
$(function() {
$("#foo").click(function (event) {
console.log("single click");
return false;
});
$("#foo").dblclick(function (event) {
console.log("double click");
return false;
});
function bar() { //do stuff };
});
Prototyping it, both seem to work. I guess that if they are wrapped within a function, it provides closure and may change things for scope a bit... However for a relative newb, are there pratical reasons why I might want (or not want) to systematically do one or the others? Is there a "better way" or is it rather "it depends", and if it depends... on what?
` you don't need doc.ready - sometimes it's best just to add it anyway for startup/events in case it moves in the future.
– freedomn-m May 18 '21 at 16:56