1.2.7.1.深入瞭解原始碼:jQeury(一)

  • 使語法更容易打出來

  • 跨瀏覽器

  • 操作DOM

<html>
    <head>

    </head>
    <body>
        <div id="main" class="container">
            <h1>People</h1>
            <ul class="people">
                <li>John Doe</li>
                <li>Jane Doe</li>
                <li>Jim Doe</li>
            </ul>
        </div>
        <script src="jquery-1.11.2.js"></script>
        <script src="app.js"></script>
    </body>
</html>
  • 選擇器

      //取得class是people的ul中的li
      var q = $("ul.people li")
      //印出jQuery.fn.init[3]
      console.log(q)
  • jQuery 1.12.4 原始碼

    • jQuery函數不是函數建構子, 所以不用new運算子, 而藉由呼叫函數建構子回傳物件

    • 小訣竅: 利用函數呼叫函數建構子, 可以不用使用new運算子

    • jQuery.fn是jQuery.prototype的別名

      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 );
      },
      jQuery.fn = jQuery.prototype = {
      
      // The current version of jQuery being used
      jquery: version,
      
      constructor: jQuery,
      
      // Start with an empty selector
      selector: "",
      
      // The default length of a jQuery object is 0
      length: 0,

Last updated