【问题标题】:JavaScript: How to combine two different but pretty similar functions?JavaScript:如何组合两个不同但非常相似的函数?
【发布时间】:2018-08-30 12:44:45
【问题描述】:

在这两个函数中;

  • url 路径发生变化。
  • 取决于不同的url路径;函数parameters 是变化。

我尝试了几种命名和用法来组合这些功能,但都没有成功!我怎样才能只使用一个功能?提前致谢。

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');

【问题讨论】:

    标签: javascript function parameters arguments


    【解决方案1】:

    您需要将功能合二为一吗?试试看。

    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')
    

    【讨论】:

    • 我只是编辑我的帖子:我忘记在RunTestCase() 中声明DynamicHost() 所以我不得不修改你的用法; host = host || DynamicHost();运行良好!.
    • 以及为什么您在参数中为folder 定义了FooFolderPath?我把它放在花括号里; folder = folder || FooFolderPath` 并且工作正常。
    • 这是js中的默认参数。这应该像folder = folder || FooFolderPatne 一样工作,但更明显
    • 我还检查了function Test (name, foo, folder = 'FooFolderPath', host = DynamicHost()) { 选项并删除了host = host || DynamicHost() 它看起来有效!你能检查一下吗?这是一个更优雅的选择
    • 没错!工作更优雅!但是你在url config 上错过了title 状态;)所以它应该是:url: folder + (foo ? foo + '/' : '') + title +'.T.js'
    【解决方案2】:

    看起来foo 参数是不同的。我会使用它,但您需要更改参数顺序:

    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
      };
    }
    

    【讨论】:

    • 在一个问题中有这样一个函数调用:RunTestCases('NameParam', 'FooParam') 使用您的代码,它将是这样的:RunTestCases('NameParam', null, null, 'FooParam') 但第二个变体是可以的:RunTestCases ('NameParam', 'BarFolderPath', ' BarHostParam')
    • 正如我所说,“您需要更改参数顺序”,OP 需要进行更改才能使其正常工作。其他所有可能的答案也是如此。 @ИльяЗеленько
    【解决方案3】:

    在两个函数中保持参数顺序相同,然后最后在参数中添加foo,然后执行如下操作:

    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'));

    【讨论】:

    • 如果您先创建对象然后有条件地添加 url 属性,这可能会更干净。这使您可以减少重复和单个返回语句。你也错过了他们的 DynamicHost 电话
    • @Marie 同意你的看法。请立即查看
    • @VicJordan 我已经修改了一些东西以实现我的目标并且它起作用了host = host || DynamicHost();if (foo !== undefined) {。不想要的事情是我不得不使用null 调用函数以进行某些使用:( 作为RunTestCases('nameparam', null, null, 'fooparam'))。所以我可以避免以某种方式使用null 吗?
    • @VicJordan 我只是编辑我的帖子:我忘记在RunTestCase() 中声明DynamicHost()
    • @NuriEngin 无需通过null。请检查我更新的答案。最后一个案例失败了,因为它正在寻找 DynamicHost() 函数,否则如果代码中存在 DynamicHost(),它也将起作用。
    【解决方案4】:
    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;
    }
    

    【讨论】:

    • 您可以在 url 分配之前增加文件夹,这样您就不必在集合中包含该逻辑。它应该可以节省您复制扩展字符串。
    • 您可以在分配之前修改文件夹值,例如if (!foo) folder = folder + foo + '/'; 然后只需设置 url 例如returnVal.url = folder + name +'.T.js';
    • 你的三元条件不是混合了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 2017-11-04
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多