3

I would like to ask the meaning of the following code:

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

These are lines 2036 - 2037 of jQuery plugin file 'jquery.datepick.js' from

http://keith-wood.name/datepick.html

  • http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work – Guilherme Nov 14 '13 at 13:29
  • 1
    possible duplicate of [What's the use of Array.prototype.slice.call(array, 0)?](http://stackoverflow.com/questions/5145032/whats-the-use-of-array-prototype-slice-callarray-0) – MackieeE Nov 14 '13 at 13:29
  • 1
    As with the answer @MackieeE linked to, slice needs to be called via Array.prototype.slice.call because the variable -- `arguments` in this case -- is not actually an array. `arguments` is similar to an array, but doesn't have all the array methods such as slice. – Matt Browne Nov 14 '13 at 13:36
  • The full function look like this: – user2990981 Nov 14 '13 at 13:50

1 Answers1

0

The full code look like this:

/* 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 || {});
        }
    });
};