In JavaScript $ (function () {}); What's the difference between writing and not writing _javascript tips

Source: Internet
Author: User
Tags script tag

in JavaScript $ (function () {...}) Is the classic use of jQuery, equivalent to $ (document). Ready (function () {...}), that is, after the page has finished loading to execute a function, if you want to manipulate the DOM in the function, it will be more secure after the page load is completed. So this is a common way to use JQuery.

$ (document). The code in Ready () is executed when the content of the page is loaded, and if the code is written directly into the script tag, when the page loads the script tag executes the code inside, At this point, if the code you are executing in the tag calls the code or DOM that is not currently loaded, the error will be, of course, if you put the script tag on the end of the page, then there is no problem, and ready effect.

$ (document). Ready (function () {}) can be simply written as $ (function () {});

Click paragraph backward, this paragraph is hidden:

 
 

If the $ (document). Ready (function () {}) is removed, the paragraph cannot be hidden:

 
 

But when you put the script at the end of the page, you can restore the hidden effect:

 
 

What is the role and usage of JavaScript (function () {})

It's okay with the object.
(function () {}) () represents an immediate method of executing an anonymous
Generally used to isolate from the outside world creating a closed-loop environment create a scope chain to avoid variable conflicts

(function () {
 var A;
..........
}) ()

This article mainly introduces the $ (function () {}) in JavaScript, what is the difference between writing and writing, and I hope it will help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.