简体   繁体   中英

Javascript What is the difference between following lines of code

What is the difference between the following lines of codes, When we are using jquery. Is there anyone of them more preferable?

<script>
alert("hello");  // 1. alert hello 
$(function(){alert("hello");}); // 2. Also alert hello
(function($){alert("hello");})(jQuery); // 3. It also alert hello
</script>
  • In the first line, you are just calling alert() .

  • In the second line, you are trying to get a collection of matched elements either found in the DOM based on passed argument(s) or created by passing an HTML string ( more here ). So the parameter is evaluated and alert() is called.

  • In the third line, you are calling a self invoking function and passing jQuery as parameter but not using it. and alert() is called.

TO SUM UP

The first line alert("hello"); is definitely the best, as the last two lines are causing useless computations just to alert.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM