Can you explain this jQuery method?

Trying to understand how jquery works under covers, what is the difference between:

jQuery.fn and jQuery.prototype

jQuery = window.jQuery = window.$ = function( selector, context ) {
        // The jQuery object is actually just the init constructor 'enhanced'
        return new jQuery.fn.init( selector, context );
},

and then:

jQuery.fn = jQuery.prototype = {
    init: function( selector, context ) {
+3
source share
3 answers

The first snippet ensures that your use of jQuery will always call the init function in the jQuery prototype.

$() == jQuery() == new jQuery() == new jQuery.prototype.init()

The second snippet allows you to access the jQuery prototype from fn. fnWhen assigning functions to a property , they are also assigned to $ .prototype. This allows you to call these methods on objects returned from $:

jQuery.fn.example = function() { alert(this.id); }
$('#some-id').example(); // alerts 'some-id'
+8
source

jQuery.fn - , jQuery. , ( jQuery!) jQuery :

jQuery.fn.myMethod = function() {...}

jQuery !

  • jQuery.fn
  • jQuery.prototype
  • jQuery.fn.init.prototype
  • jQuery.prototype.init.prototype

: jQuery $ () , jQuery $ , 8 jQuery.fn.

? , .

jQuery.fn , jQuery, jQuery, . , , , , "fn" .

jQuery.prototype, , jQuery, , . , , jQuery :

obj = new jQuery(selector);

jQuery() , , , - jQuery.fn.init(). jQuery.prototype . , - jQuery.fn jQuery.prototype .

jQuery.fn.init.prototype. , jQuery.fn.init(). jQuery, , 'prototype' , 'this'.

, jQuery.prototype.init.prototype - , jQuery.fn jQuery.prototype . .

, 4 jQuery, . , jQuery.fn - jQuery.fn.init() . , jQuery $.

jQuery()
new jQuery()
new jQuery.fn.init()
new jQuery.prototype.init()

jQuery . , API- DOM. , jQuery , , .

+4

, , - , jQuery ( javascript - ).

jQuery.fn jQuery, . prototype, jQuery.

http://mckoss.com/jscript/object.htm Javascript OOP

, ...

+3

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


All Articles