jQuery插件教程解释

时间:2013-05-02 14:51:04

标签: javascript jquery jquery-plugins

我正在关注JQuery Plugins/Authoring教程,无法弄清 16 18 的行arguments是什么意思。我错过了一些非常基本的东西吗?

(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 );

谢谢。

2 个答案:

答案 0 :(得分:1)

arguments是一个类似数组的对象,它包含传递给函数的参数,包括您没有为其提供变量名称的参数。

它类似于数组,但不是数组。它不包含任何数组方法,例如切片,这就是您必须使用Array.prototype.slice.call(arguments,...)[].slice.call(arguments,...)而不是仅使用arguments.slice(...)

的原因

答案 1 :(得分:0)

arguments是一个JavaScript保留关键字,它是一个包含传递给函数的所有参数的数组。

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

相关问题