Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript以下代码行之间的区别是什么_Javascript_Jquery - Fatal编程技术网

Javascript以下代码行之间的区别是什么

Javascript以下代码行之间的区别是什么,javascript,jquery,Javascript,Jquery,当我们使用jquery时,以下代码行之间的区别是什么。他们中有谁更可取吗 <script> alert("hello"); // 1. alert hello $(function(){alert("hello");}); // 2. Also alert hello (function($){alert("hello");})(jQuery); // 3. It also alert hello </script> 警报(“你好”);//1.警惕你好 $(函数(

当我们使用jquery时,以下代码行之间的区别是什么。他们中有谁更可取吗

<script>
alert("hello");  // 1. alert hello 
$(function(){alert("hello");}); // 2. Also alert hello
(function($){alert("hello");})(jQuery); // 3. It also alert hello
</script>

警报(“你好”);//1.警惕你好
$(函数(){alert(“hello”);});//2.也提醒你好
(函数($){alert(“hello”);})(jQuery);//3.它也提醒你好
  • 在第一行中,您只需调用
    alert()

  • 在第二行中,您试图获取匹配元素的集合,这些元素要么是基于传递的参数在DOM中找到的,要么是通过传递HTML字符串()创建的。因此,将计算参数并调用
    alert()

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

总结
第一行
警报(“hello”)
绝对是最好的,因为最后两行只是为了提醒而导致了无用的计算。

可能的重复只是编写一个自调用函数的一种方式,而不是在所有地方显示“hello”,alert
this
,如
alert(this)
,您将了解更多信息。