Tester232323 Tester232323 - 2 years ago 117
jQuery Question

Pass through arugments object

I want to perfectly wrap jquery post and get. Is it somehow possible to pass through the arguments object?

function myPost(){
return $.post(arguments);

Doing the above doesn't work because it is passed as an object.

Otherwise I have to wrap this somehow

jQuery.each( [ "get", "post" ], function( i, method ) {
jQuery[ method ] = function( url, data, callback, type ) {
// shift arguments if data argument was omitted
if ( jQuery.isFunction( data ) ) {
type = type || callback;
callback = data;
data = undefined;

return jQuery.ajax({
url: url,
type: method,
dataType: type,
data: data,
success: callback

Answer Source

You are looking for apply():

function myPost() {
    return $.post.apply($, arguments);

With apply you can pass an array, which will be used for calling the function with the first argument set to the first array element, the second to the second, etc.

ES6 Spread Operator

With the ES6 spread operator you can do it like this:

function myPost() {
    return $.post(...arguments);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download