[英]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.