> For the complete documentation index, see [llms.txt](https://jen-hsuan-hsieh.gitbook.io/javascript-node-js/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jen-hsuan-hsieh.gitbook.io/javascript-node-js/chapter1/12javascript-quan-gong-lve-ff1a-ke-fu-js-de-qi-guai-bu-fen/127jian-yan-zhi-ming-de-kuang-jia-yu-zi-yuan-ku/1272shen-ru-liao-jie-yuan-shi-ma-ff1a-jqeury-ff08-er-ff09.md).

# 1.2.7.2.深入瞭解原始碼：jQeury（二）

* [sizzle](http://sizzlejs.com/)
  * Jquery 裡的另一個eningee: 給他一個字串他會找到HTML
  * 所以傳入selector的字串, 可以用sizzle做到
* jQuery的函數建構子
  * 呼叫[new運算子](https://jenhsuan.gitbooks.io/javascript-node-js/content/chapter1/125han-shu-jian-gou-zi-3001-300c-new-300d-yu-javascript-de-li-shi/1251han-shu-jian-gou-zi-3001-300c-new-300d-yu-javascript-de-li-shi.html)會建立新的空物件, 如果不自己回傳值, 他會自己回傳
  * 如果把一個函數當成建構子, this將會指向被new運算子建立的空物件
  * 參考jQuery的函數建構子的作法, 可以將this變數傳入另一個函數做處理, 接著從函數建構子回傳

    ```
    init = jQuery.fn.init = function( selector, context, root ) {
      var match, elem;

      // HANDLE: $(""), $(null), $(undefined), $(false)
      if ( !selector ) {
          return this;
      }

      // init accepts an alternate rootjQuery
      // so migrate can support jQuery.sub (gh-2101)
      root = root || rootjQuery;

      ...
      // HANDLE: $(function)
      // Shortcut for document ready
      } else if ( jQuery.isFunction( selector ) ) {
          return typeof root.ready !== "undefined" ?
              root.ready( selector ) :

              // Execute immediately if ready is not present
              selector( jQuery );
      }

      if ( selector.selector !== undefined ) {
          this.selector = selector.selector;
          this.context = selector.context;
      }

      return jQuery.makeArray( selector, this );
    };
    // Give the init function the jQuery prototype for later instantiation
    init.prototype = jQuery.fn;
    ```
* 建立一個jQuery物件時, 實際上jQuery做了甚麼事
  * 1.呼叫函數用new關鍵子呼叫函數建構子接著建立新物件

    ```
    jQuery = function( selector, context ) {

      // The jQuery object is actually just the init constructor 'enhanced'
      // Need init if jQuery is called (just allow error to be thrown if not included)
      return new jQuery.fn.init( selector, context );
    },
    init = jQuery.fn.init = function( selector, context, root ) {
    ...
    }
    ```
  * 2.新物件可以取用這裡所有的屬性與方法

    * 將新物件的原型指向函數的.prototype屬性

    ```
    // Give the init function the jQuery prototype for later instantiation
    init.prototype = jQuery.fn;
    ```
