xcorat
xcorat

Reputation: 1472

jQuery plugin tutorial explanation

I'm following the JQuery Plugins/Authoring tutorial, and couldn't figure out what arguments on lines 16 and 18 mean. Am I missing something really fundamental?

(function( $ ){

var methods = {
    init : function( options ) { 
    // ... 
    },
    show : function( ) {
    // ...
};

$.fn.tooltip = function( method ) {

    // Method calling logic
    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.tooltip' );
    }    

};

})( jQuery );

Thank you.

Upvotes: -1

Views: 201

Answers (2)

Kevin B
Kevin B

Reputation: 95028

arguments is an array-like object that contains the parameters that were passed into the function, including parameters that you didn't supply a variable name for.

It is array-like, but not an array. It does not contain any of the array methods, such as slice, which is why you have to use Array.prototype.slice.call(arguments,...) or [].slice.call(arguments,...) rather than just using arguments.slice(...)

Upvotes: 1

mpj
mpj

Reputation: 5367

arguments is a JavaScript reserved keyword, which is an array containing all arguments passed to a function.

http://msdn.microsoft.com/en-us/library/ie/he95z461(v=vs.94).aspx

Upvotes: 0

Related Questions