简体   繁体   English

Javascript 以下几行代码有什么区别

[英]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.以下几行代码有什么区别,当我们使用 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() .在第一行,您只是调用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 ).在第二行中,您试图根据传递的参数在 DOM 中找到或通过传递 HTML 字符串创建匹配元素的集合(更多信息请点击此处)。 So the parameter is evaluated and alert() is called.因此评估参数并调用alert()

  • In the third line, you are calling a self invoking function and passing jQuery as parameter but not using it.在第三行中,您正在调用一个自调用函数并将 jQuery 作为参数传递但不使用它。 and alert() is called.alert()被调用。

TO SUM UP总结

The first line alert("hello");第一行alert("hello"); is definitely the best, as the last two lines are causing useless computations just to alert.绝对是最好的,因为最后两行只是为了提醒而导致无用的计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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