i’m reading through jQuery’s “Plugins/Authoring” though i already wrote a few jQuery-Plugins. Now I see that jQuery has a special way of scoping the methods and calling:
(function( $ ){
var methods = {
init : function( options ) { // THIS },
show : function( ) { // IS },
hide : function( ) { // GOOD },
update : function( content ) { // !!! }
};
$.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 );
I understand the concept of what will happen in the end… but how exactly? This part is what confuses me:
// 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 );
}
Why Array.prototype.slide.call(argumetns, 1)? And where does the variable “arguments” come from all of the sudden? Any brief or deeper explanation is much appreciated. It is said, that this is how plugins should be written… so i’d like to know why.
Thanks!
argumentsargumentsis a part of the JavaScript language. I was confused in exactly the way you were when I first ran into it; it’s not just you. 🙂 It’s an automatic local variable in every function, and is an array-like structure giving you all of the arguments (see Section 10.6 of the spec), e.g.:When I say
argumentsis array-like, I mean it — it’s not an Array. Its references to the arguments are live (and bidirectional). For instance:Note that when assigning a value to
namedArg, the result is reflected inarguments[0], and vice-versa.argumentsis really cool, but only use it if you need to — some implementations speed up calling functions by not hooking it up until/unless the function actually first tries to access it, which can slow the function down (very slightly).argumentsalso has property on it calledcallee, which is a reference to the function itself:However, it’s best to avoid using
arguments.calleefor several reasons. One reason is that in many implementations, it’s really slow (I don’t know why, but to give you an idea, the function call overhead can increase by an order of magnitude if you usearguments.callee). Another reason is that you can’t use it in the new “strict” mode of ECMAScript5.(Some implementations also had
arguments.caller— shudder — but fortunately it was never widespread and is not standardized anywhere [nor likely to be].)The
slicecall andapplyRegarding
What that’s doing is using the
Array#slicemethod to copy the arguments into an array (minus the first argument, which was the method to call), and then passing the resulting array into theFunction#applyfunction on the function instance it’s calling.Function#applycalls the function instance with the giventhisobject and the arguments supplied as an array. The code’s not just usingarguments.slicebecause (again)argumentsisn’t really an Array and so you can’t rely on it having all of the Array functions, but the specification specifically says (in Section 15.4.4.10) that you can apply theArray.prototype.slicefunction to anything that’s array-like, and so that’s what they’re doing.Function#applyandFunction#callare also built-in parts of JavaScript (see Sections 15.3.4.3 and 15.3.4.4). Here are simpler examples of each: