> 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/1273shen-ru-liao-jie-yuan-shi-ma-ff1a-jqeury-ff08-san-ff09.md).

# 1.2.7.3.深入瞭解原始碼：jQeury（三）

* 方法鏈結 (method chaining)

```
//取得class是people的ul中的li
var q = $("ul.people li").addClass("newclass").removeClass("people")
//印出jQuery.fn.init[3]
console.log(q)
```

* 原始碼
  * this會指向jQuery物件

    ```
    addClass: function( value ) {
      var classes, elem, cur, curValue, clazz, j, finalValue,
          i = 0;

      if ( jQuery.isFunction( value ) ) {
          return this.each( function( j ) {
              jQuery( this ).addClass( value.call( this, j, getClass( this ) ) );
          } );
      }

      if ( typeof value === "string" && value ) {
          classes = value.match( rnotwhite ) || [];

          while ( ( elem = this[ i++ ] ) ) {
              curValue = getClass( elem );
              cur = elem.nodeType === 1 &&
                  ( " " + curValue + " " ).replace( rclass, " " );

              if ( cur ) {
                  j = 0;
                  while ( ( clazz = classes[ j++ ] ) ) {
                      if ( cur.indexOf( " " + clazz + " " ) < 0 ) {
                          cur += clazz + " ";
                      }
                  }

                  // only assign if different to avoid unneeded rendering.
                  finalValue = jQuery.trim( cur );
                  if ( curValue !== finalValue ) {
                      jQuery.attr( elem, "class", finalValue );
                  }
              }
          }
      }

      return this;
    },
    ```
