3

According to this StackOverflow answer What does jQuery.fn mean?, the fn property in jQuery.fn.jquery is an alias to the prototype property. I assume that this would be the same in these two methods whose full code is below

$.fn.map = function() and $.fn.tweets = function()

My question then, is, if, for example, $.fn.tweets uses the prototype to create a tweets method, would this code with $('tweets').tweets be calling it...

var $tweets = $('#tweets').tweets({
        query: buildQuery(approxLocation),
        template: '#tweet-template'
    });

and, if so, how might it trigger that method. For example, does the mere creation of the variable on file loading trigger that function, which has other methods inside of it, namely query? Thanks for your help

Full code of methods

  $.fn.map = function(method) {
         console.trace();
         console.log(method);
        if (method == 'getInstance') {
            console.log("fn.map");
            return this.data('map');
        }

        return this.each(function() {
            var $this = $(this);
            var map = $this.data('map');

            if (map && MyMap.prototype[method]) {
                 map[method] (Array.prototype.slice.call( arguments, 1 ));
            } else if ( typeof method === 'object' || ! method ) {
                var options = method;
                $this.data('map', new MyMap( this, options ));
            } else {
                $.error( 'Method ' +  method + ' does not exist on jQuery.map' );
            }
        });
    }

   $.fn.tweets = function(method) {

        if ( methods[method] ) {
            return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {

            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.tweets' );
        }
    }

variables that call those methods?

 var $tweets = $('#tweets').tweets({
        query: buildQuery(approxLocation),
        template: '#tweet-template'
    });
var $map = $('#map').map({
    initialLocation: approxLocation,
    radius: 1000,
    locationChanged: function(location) {
        $tweets.tweets('setQuery', buildQuery(location));
    }
});
Community
  • 1
  • 1
BrainLikeADullPencil
  • 11,313
  • 24
  • 78
  • 134

1 Answers1

10

Firstly, prototypes are just objects. In this case, yes:

jQuery.prototype === jQuery.fn

So saying jQuery.fn.map = function() {} is like saying jQuery.prototype.map = function() {}

When you instantiate a new jquery object with $(selector | dom node | ...) you are returning a jQuery object which automatically inherits all the prototype methods, including map, tweet, etc. Research Javascript's prototypal inheritence model and how object prototypes work in regard to new

$ is just a reference to jQuery which returns a specially modified new object. $ is a function which returns a new object reference. Here's a simplified example (but you really should research more about prototypal inheritence, it has been answered many times repeatedly):

var A = function() {
};

A.prototype.doThing = function() {
};

var newObj = new A();

newObj.doThing // this new object has this method because it's on A's prototype

so newObj.doThing is just like $(selector).tweet

Also feel free to read the source of jQuery and trace what happens when a new object is created. You can see near the top exactly what is happening under the comment // Define a local copy of jQuery

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
Andy Ray
  • 30,372
  • 14
  • 101
  • 138
  • thanks, so does the creation of this variable upon file loading trigger that prototype method? In the code I provided, it doesn't use the 'new' constructor. var $tweets = $('#tweets').tweets({ – BrainLikeADullPencil Nov 07 '12 at 00:20
  • @user1647484: Yet, right inside the `$` function, an instance of the actual jQuery constructor is created via `new` and returned. Have a look at [this answer](http://stackoverflow.com/a/12143833/1048572) for the internals – Bergi Nov 07 '12 at 00:37