jquery implements $.fn.extend and $.extend functions _jquery

Source: Internet
Author: User
Tags extend object object

We extended the Bind method and the Ready function, and this time I'll talk about the $.fn.extend and $.extend functions.

Other not to say, directly into the theme bar!

Let's take a look at the difference between the two functions:

$.fn.extend is a method for node object extension of query, and is a method of prototype extension based on $

$.extend is a static method that extends the normal method and is $.

Let's look at the code we wrote before:

   (function (Win) {
      var _$ = function (selector, context) {return
        new _$.prototype. Init (selector, context);
      }
      _$.prototype = {
        Init:function (selector, context) {
  
        },
        Each:function (callback) {
 
        }      
      }
      _$. Prototype. Init.prototype = _$.prototype;       
      window.$ = window. JQuery = _$;
    }) (window);

This is the code for the principal.

I'm going to expand the $.fn.extend method first:

The idea of this method is that we can use $ ("") Newmetod () to access it after we extend it, which is actually adding a extend method to the $ prototype. The FN in the middle is actually similar to the role of the namespace, with little practical significance. In order to make a distinction with $.extend.

Familiar with the prototype of the fact that a look at it: let $.fn point to the prototype of $ not on the line?

So we have the following code: _$.FN = _$.prototype;

Next we'll add the Extend method:

  var isobj = function (o) {return
      Object.prototype.toString.call (o) = = "[Object Object]";
    }
    _$.fn.extend = function (obj) {
      if (isobj (obj)) {for
        (var i in obj) {
          this[i] = Obj[i];
        }
    }

The role of isobj in this code is to determine whether the incoming parameters are object objects, _$.fn.extend This method is actually the same as _$.prototype.extend, you see, this code may not be the same as the jquery source, I wrote it according to my own meaning.

Here's how to implement the $.extend method, which has just been said, is actually a static method for $, the code is as follows:

    $.extend = function (obj) {
        if (isobj (obj)) {for
          (var i in obj) {
            this[i] = Obj[i];
          }
      }

You will find that two methods are the same, but you have to think about it differently:

_$.fn.extend inside the This is actually representing $.prototype, $.extend inside of this represents is $.

These two methods have been implemented, with all the code:

 (function (Win) {
      var _$ = function (selector, context) {return
        new _$.prototype. Init (selector, context);
      }
      _$.prototype = {
        Init:function (selector, context) {

        },
        Each:function (callback) {

        }
      }
      _$. Prototype. Init.prototype = _$.prototype;
      _$.fn = _$.prototype;
      var isobj = function (o) {return
        Object.prototype.toString.call (o) = = "[Object Object]";
      }
      _$.fn.extend = function (obj) {
        if (isobj (obj)) {for
          (var i in obj) {
            this[i] = Obj[i];
          }
      }
      _$.extend = function (obj) {
        if (isobj (obj)) {for
          (var i in obj) {
            this[i] = Obj[i];
          }
      }
      window.$ = window. JQuery = _$;
    }) (window);

The use of the method is actually

$.fn.extend (

{

   method:function () {

}

})

$.extend (

{

   method:function ()

{}})

Code and jquery source is not the same, I this is to simplify the method of writing, we are mainly to ponder the idea inside, thank you for your reading.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.