JQuery extends the syntax of $ .fn and method invocation

I understand that you are calling a method like this when you expand $.fn.

( $ == jQuery)

//$("div").myMethod();

$.fn.extend({
   myMethod: function(){...}
});

And how is it when you extend the jQuery object:

//$.myMethod2();

$.extend({
    myMethod2: function(){...}
});

But I do not quite understand what makes $():

// Several methods are called here. Some with the same method name (initiate), which is bad.

// But perhaps that’s why you need an empty jQuery object, so method names don’t collide

$(function() {

    $().functionName({
      something: 'something'
    }).myMethod();

    $().func1({
      x: 1
    }).initiate();

    $().func2({
      y: 2
    }).initiate();

});

Is this because the method is being called, including the name of the function?

Thanks in advance and hope this makes sense.

+3
source share
2 answers

$() jQuery ( ). , - - . , functionName myMethod = .

jQuery().css({
   'foo': 'bar'
}).show();
+3

jQuery 1.4 jQuery() , jQuery(document). , . jQuery 1.4, jQuery() jQuery([]). , , -.

, , , , document node, , / , . $(), , $ $(document).

, jQuery . .

, , jQuery:

(function($){
    $.myMethod = function() {
    };

    $.fn.myMethod = function() {
    };
})(jQuery);
+1

Source: https://habr.com/ru/post/1784543/


All Articles