简体   繁体   中英

JavaScript: How to combine two different but pretty similar functions?

Within those two functions;

  • url path is changes.
  • Depends on different url path; function parameters are changes.

I've tried several naming and usage to combine those functions but couldn't success! How can I use only a function? Thanks in advance.

function RunTestCases (name, foo, folder, host) {
    host = host || DynamicHost();
    folder = folder || 'FooFolderPath';

    return {
        title: name,
        hostPageUrl: host,
        url: folder + foo + '/'+ name +'.T.js'
    };
}

function RunMonkeyTestCase (name, folder, host) {
    host = host || DynamicHost();
    folder = folder || 'FooFolderPath';

    return {
        title: name,
        hostPageUrl: host,
        url: folder + name +'.T.js'
    };
}

//Usage of Functions;
RunTestCases('NameParam', 'FooParam');
RunMonkeyTestCase('NameParam', 'BarFolderPath', 'BarHostParam');

//For some specific usages.
RunTestCases('NameParam', 'FooParam', 'BarFolderPath', 'BarHostParam');
RunMonkeyTestCase('NameParam', null, 'FooHostParam');

Do you need to combine functions into one? Try it.

function Test (title, foo, folder = 'FooFolderPath', hostPageUrl = DynamicHost()) {
  return {
    title,
    hostPageUrl,
    url: folder + (foo ? foo + '/' : '') + title + '.T.js'
  };
}

//Usage of Functions;
Test('NameParam', 'FooParam')
Test('NameParam', null, 'BarFolderPath', 'BarHostParam')

Keep the parameter order same in both functions and then at last add foo in parameter and then do something like below:

 function TestCase(name, folder, host, foo) { host = host || DynamicHost(); folder = folder || 'FooFolderPath'; let url; if (foo) { url = folder + foo + '/' + name + '.T.js'; } else { url = folder + name + '.T.js' } return { title: name, hostPageUrl: host, url: url }; } console.log(TestCase('NameParam', 'BarFolderPath', 'BarHostParam', 'FooParam')); console.log(TestCase('NameParam', 'BarFolderPath', 'BarHostParam')); console.log(TestCase('NameParam', 'FooParam', 'BarFolderPath', 'BarHostParam')); console.log(TestCase('NameParam', 'FooHostParam')); 

It looks like the foo parameter is the distincted one.. I would use that, but you need to change the params order :

function RunTestCases (name, folder, host,foo) 
  {
  host = host || (foo? 'FooHostParam' : DynamicHost()) ;
  folder = folder || foo? 'FooFolderPath' : 'BarFolderPath')
  const url = (foo? (folder + foo + '/' + name +'.T.js') : (folder + name +'.T.js'));
  return {
      title: name,
      hostPageUrl: host,
     url
  };
}
function RunTest (name, folder, host, foo) {
    host = host || (foo ? DynamicHost() : 'FooHostParam');
    folder = folder || 'FooFolderPath';

    returnVal = {
        title: name,
        hostPageUrl: host,
    };

    returnVal.url = foo ? folder + name +'.T.js' : folder + foo + '/'+ name +'.T.js';

    return returnVal;
}

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