简体   繁体   中英

Optional arguments in javascript or reordered

Is there any simple way to treat the arguments based on entity?

On my function all depends if the second param or third is a function, an object and so. This is my function:

function fbAPI(endpoint, param1, param2, param3, param4) {

    var type, params, success, error;

    if(typeof param1 !== 'function' && typeof param2 !== 'function')
    {
        type    = param1;
        params  = param2;
        success = param3;
        error   = param4;
    }

    if(typeof param1 === 'function')
    {
        type    = 'GET';
        params  = {};
        success = param1;
        error   = param2;
    }

    if(typeof param1 === 'object')
    {
        type    = 'GET';
        params  = param1;
        success = param2;
        error   = param3;
    }

    if(typeof param1 !== 'function' && typeof param2 === 'function')
    {
        type    = param1;
        params  = {};
        success = param2;
        error   = param3;
    }

    FB.api(
        endpoint,
        type,
        params,
        function (response) {

            if (response && !response.error) {
                /* handle the result */
                if(typeof success === 'function')
                    success(response);
            }

            if (response && response.error) {
                console.log(response.error);
                /* handle the result */
                if(typeof error === 'function')
                    error(response);
            }
        }
    );
}

Is there any way to make it shorter?

I should can call my function this way:

    this.api(_self.endPoints.friends, function (response) {
       //do something
    });

    this.api(_self.endPoints.friends, function (response) {
       //do something
    },function (response) {
       //do something
    });

    this.api(_self.endPoints.friends, 'GET', function (response) {
       //do something
    },function (response) {
       //do something
    });

    this.api(_self.endPoints.friends, {data: data}, function (response) {
       //do something
    },function (response) {
       //do something
    });

    this.api(_self.endPoints.friends, 'GET', {data: data}, function (response) {
       //do something
    },function (response) {
       //do something
    });

Maybe try something like this and decide what to do when calling your function

function example(opts) {
    // Do whatever you want with the options here
}


example({"type":"GET", "param":"[\"something\",\"here\",\"too\"]", "success":"yay", "error":"boo"});

Don't design this type of function signature. It's a nightmare to handle, and it will also end up confusing your users. You also will end up with odd situations where fbAPI(endpoint, 'GET', success, error) fails because you didn't think of defaulting params in the case that the type is given.

If you insist, then it's easier if you think of the arguments as an array that you shift things off the front of:

function fbAPI(endpoint, ...args) {

    var 
      type    = typeof args[0] === 'string' ? args.shift() : 'GET', 
      params  = typeof args[0] === 'object' ? args.shift() : {},
      success = args.shift(),
      error   = args.shift();

The way You are doing it is a bit strange but You can use an array for that. Example:

var t = {
    // typeof param1
    'function': {
        // typeof param2
        'function': ['GET', param1, param2],
        'object': ['GET', param3, param2],
    },
    'object': {
        'function':['GET', param1, param3],
        'object': 'error',
    }
};


var r = t[typeof param1][typeof param2];

Hope it helps.

Because you're using JavaScript, I would avoid having multiple methods to call an internal method. That's called method overloading, and that is inherent to JavaScript unlike how it is in other languages like Java or C#.

There are a few ways you can do what you're looking for, though the way you have it is only verbose. One thing I would do is to make them if / else if / else statements instead of all if s so that it is immediately recognizable that you can't go into multiples. I would also reorder so that they flow through a little logically.

var type, params, success, error;

if(typeof param1 === 'function') {
    type    = 'GET';
    params  = {};
    success = param1;
    error   = param2;
} else if(typeof param1 === 'object') {
    type    = 'GET';
    params  = param1;
    success = param2;
    error   = param3;
} else if(typeof param1 !== 'function' && typeof param2 === 'function') {
    type    = param1;
    params  = {};
    success = param2;
    error   = param3;
} else  if(typeof param1 !== 'function' && typeof param2 !== 'function') {
    type    = param1;
    params  = param2;
    success = param3;
    error   = param4;
}

However, you could also just use the arguments array to make things easier. You could also check the length of the array to ensure you at least have all of the parameters you're looking for.

function fbAPI(endpoint) {
    var type, params, success, error;

    if(typeof arguments[0] === 'function' && arguments.length === 2) {
        type    = 'GET';
        params  = {};
        success = arguments[0];
        error   = arguments[1];
    } else if(typeof param1 === 'object' && arguments.length === 3) {
        type    = 'GET';
        params  = arguments[0];
        success = arguments[1];
        error   = arguments[2];
    } else if(typeof param1 !== 'function' && typeof param2 === 'function' && arguments.length === 3) {
        type    = arguments[0];
        params  = {};
        success = arguments[1];
        error   = arguments[2];
    } else  if(typeof param1 !== 'function' && typeof param2 !== 'function' && arguments.length === 4) {
        type    = arguments[0];
        params  = arguments[1];
        success = arguments[2];
        error   = arguments[3];
    }
    // ... the rest of your function
}

Finally, what would probably work best is to have your parameter be an object, and let callers set up what they want.

function fbAPI(endpoint, settings) {
    var type = settings.type || 'GET';
    var params = settings.params || {};
    var success = settings.success;
    var error = settings.error;

    // Go about your business...
}

There are many ways to such things, find below 3 ways:

 // Destructuring Assignment function test1Fun ({ name, val }) { console.log(name, val); } // Extended Parameter Handling function test2Fun (...params) { console.log(params.length); console.log(params[0]); console.log(params[1]); console.log(params[2]); } // Key-value function test3Fun (options) { console.log(options.name); } var options = { name: "bar", val: 42 }; test1Fun(options); test2Fun(1, 2, 3, 4, 5); test3Fun(options); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM