jQuery封装在Javascript库中的含义是什么?

What is the meaning of jQuery enclosure in Javascript libraries?

本文关键字:是什么 封装 Javascript jQuery      更新时间:2023-09-26

我看到很多第三方javascript代码像这样被jQuery包围:

$(function() {
  // ... javascript code
});

用jQuery封装的目的是什么?

这和$(document).ready(function() { /* ... */ });不同,对吧?

这和$(document).ready(function() { /* ... */ } );不同,对吧?

不,这只是速记。它们是完全一样的,你可以在jQuery的网站上看到:

以下三种语法都是等价的:

  • $(document).ready(handler)
  • $().ready(handler)(不建议这么做)
  • $(handler)