jQuery core objects, prototype objects, static methods, dynamic method

What is jQuery core object?

  $    $===jQuery

What is jQuery's prototype object?

  $.fn    $.fn===$.prototype

 

What is a static method?

  In the trees defined constructor method, a static method to call the constructor. For example: Math.random ()

What is the dynamic method?

  Dynamic method is also known as an instance method, the method added in the prototype object called dynamic methods, by instance objects to call. For example: arr.slice ()

 

JQuery static methods and dynamic methods?

  JQuery extensions to time, there are two ways, one is $ .extend () of methods for a $ .fn way, $. Extend () extension is a static method, $. Fn extension is dynamic method.

  Static method using jQuery core object to call, that is, to call with $, dynamic method with jQuery's prototype object to call, that is, with a $ .fn to call.

 

  Now jQuery extension method to get a random color:

    (function($){
      $.extend({
        randomColor:function(){
          var col="rgba(";
          for(var i=0;i<3;i++){
            col+=Math.floor(Math.random()*256)+",";
          }
          col+=Math.random().toFixed(2)+")";
          return col;
        }
      });
    })($);
  This is the expansion of a static method, use $ to call when in use. Div setting such a random color, $ ( "div") CSS ( "the backgroundColor",. $ .RandomColor );
 
  JQuery now to expand a get / set the background color of methods:
    (function($){
      $.fn.bgc=function(color){
        if(!color) return this.css("backgroundColor");
        this.css("backgroundColor",color);
        return this;
      }
    })($);
  This is the expansion of a dynamic method, with the object to call jQ use. Div settings such as background color, $ ( "div") .bgc ( "Red") ; this is $ .fn way, bgc () is the function fn.

   

  In other words, use $ .extend () jQuery is to add a static method call with $, $ .fn is to use JQuery to add a dynamic method, .fn called with $ use use .

$ Called core object, $. Fn called a prototype object.

  typeof $--------->function

  typeof $.fn--------->object

  $ Equivalent class, a class method called directly, $. Fn corresponding to the object instance, the object instance to invoke a method.

 

Guess you like

Origin www.cnblogs.com/wuqilang/p/11206630.html