jQuery JavaScript 的含义:Array.prototype.slice.call

3

我想请教以下代码的含义:

$.fn.datepick = function(options) {
    var otherArgs = Array.prototype.slice.call(arguments, 1);

这是jQuery插件文件“jquery.datepick.js”的第2036-2037行。
来自http://keith-wood.name/datepick.html

https://dev59.com/a3RB5IYBdhLWcg3wn4UL - Guilherme
1
可能是What's the use of Array.prototype.slice.call(array, 0)?的重复问题。 - MackieeE
1
与@MackieeE链接的答案一样,slice需要通过Array.prototype.slice.call调用,因为在这种情况下变量--arguments实际上不是一个数组。 arguments类似于数组,但没有所有的数组方法,如slice。 - Matt Browne
完整的函数看起来像这样: - user2990981
1个回答

0

完整的代码看起来像这样:

/* Attach the datepicker functionality to a jQuery selection.
   @param  options  (object) the new settings to use for these instances (optional) or
                    (string) the command to run (optional)
   @return  (jQuery) for chaining further calls or
            (any) getter value */
$.fn.datepick = function(options) {
    var otherArgs = Array.prototype.slice.call(arguments, 1);
    if (isNotChained(options, otherArgs)) {
        return plugin['_' + options + 'Plugin'].apply(plugin, [this[0]].concat(otherArgs));
    }
    return this.each(function() {
        if (typeof options == 'string') {
            if (!plugin['_' + options + 'Plugin']) {
                throw 'Unknown command: ' + options;
            }
            plugin['_' + options + 'Plugin'].apply(plugin, [this].concat(otherArgs));
        }
        else {
            plugin._attachPlugin(this, options || {});
        }
    });
};

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接