The difference of $ .extend and $ .fn.extend

Use object-oriented thinking here to understand

First, these two methods are two methods for the development of plug-ins provided jquery

Can jquery seen as a packaged class class attributes and methods have certainly

For example $ ( "# btn1") generates an instance of an object class jquery

$ .extend (object); he is jquery  class add methods can be understood as add static method

Such as

$.extend({

minfunction(a,b){

return a<b?a:b;

},

Max:function(a,b){

Return a>b?a:b;

}

})

$ .Enxtend (target, object1, [objectN]) with one or more other objects to extend a target object return is extended

Such as

Var settings={validate:false,limit:5,name:”foo”}

Var options={validate:true ,name:”bar”}

$.extend(settings,options);

Results: Settings == {the validate: to true, limit:. 5, name: "bar"}

 

// new, more in-depth .extend () 
jQuery.extend (to true, 
{name: "John", LOCATION: {City: "Boston"}}, 
{Last: "Resig", LOCATION: {State: "MA" }} 
); 
// results // => {name: "John", Last: "Resig",  // LOCATION: {City: "Boston", State: "MA"}}  

 

$ .fn.extend (object) which is to add a member jquery method (object)

$ .fn refers to the jQuery namespace, the Fn member of the ( method of function and properties of Property) , would jQuery every instance be effective. 

View jQuery code, it is not difficult to find.

jQuery.fn = jQuery.prototype = {

   init: function( selector, context ) {//.... 

};

The original jQuery.fn = jQuery.prototype.

So, it is jQuery.prototype get in expansion, is to jQuery add class " member functions " . jQuery instance of the class can use the " member function "

E.g:

$.fn.extend({

doAlertWhileClick:function(){

$(this).click(function(){

Alert($(this).val())

})

}

})

. $ ( "# input1") doAlertWhileClick (); // page is:    

$ ( "# input1")  is a jQuery instance, when it calls member method doAlertWhileClick after they realized the extension, every time it is clicked to edit the contents of the current pop

loading………….

Guess you like

Origin www.cnblogs.com/lflying/p/10986413.html