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

4
  • stackoverflow.com/questions/572897/… Commented Nov 14, 2013 at 13:29
  • 1
    possible duplicate of What's the use of Array.prototype.slice.call(array, 0)? Commented Nov 14, 2013 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. Commented Nov 14, 2013 at 13:36
  • The full function look like this: Commented Nov 14, 2013 at 13:50

1 Answer 1

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 || {});
        }
    });
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.