繁体   English   中英

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

[英]Javascript What is the difference between following lines of code

以下几行代码有什么区别,当我们使用 jquery. 他们中有人更可取吗?

<script>
alert("hello");  // 1. alert hello 
$(function(){alert("hello");}); // 2. Also alert hello
(function($){alert("hello");})(jQuery); // 3. It also alert hello
</script>
  • 在第一行,您只是调用alert()

  • 在第二行中,您试图根据传递的参数在 DOM 中找到或通过传递 HTML 字符串创建匹配元素的集合(更多信息请点击此处)。 因此评估参数并调用alert()

  • 在第三行中,您正在调用一个自调用函数并将 jQuery 作为参数传递但不使用它。 alert()被调用。

总结

第一行alert("hello"); 绝对是最好的,因为最后两行只是为了提醒而导致无用的计算。

暂无
暂无

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

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