仅在javascript中传递第二个参数 [英] Pass only the second argument in javascript

查看:96
本文介绍了仅在javascript中传递第二个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试制作一个仅传递函数第二个参数的函数.我已经阅读了一些文档,但没有对我进行任何探索.

I try currently to make a function in which I pass only the second argument of my function. I have read some documentation but nothing probing to me.

我会做到的:

 function test (a,b){ ....};
    // pass only the second parameter 
    test( ... , b) ;

我当前的想法是将第二个参数作为动态默认参数传递,如下所示:

My current idea is to pass the second argument as a de facto dynamic default parameter as following :

    var defaultVar = "something" ;

    function test (a,b = defaultVar){...}

然后根据我的需要更改defaultVar值.

then change the defaultVar value according to my needs.

var defaultVar = modification ; 

实际上,我正在使用Google驱动器API,并且尝试在第二个参数中输入字符串值以进行回调.此回调将充当验证返回文件是否有效地被搜索到的文件的角色(通​​过对名称值进行布尔验证).

In fact, I'm working on Google drive API and I try to make to enter a string value in the second parameter to make a callback. This callback would take the role to verify if the return file is effectively the file searched (by making a boolean verification on name value).

因此,我的想法是通过传递他的名字并以此方式检索文件数据来自动化在Google驱动器上获取文件的过程.

Hence, the idea to me is to automatize the process of getting a file on Google drive by passing his name and retrieving the file data in this way.

我希望我的精确度会有所帮助.

I hope my precisions will be useful.

这是我的quickstart.js:

Here my quickstart.js :

(...Google authentication and all) ; 

 var filename = "";
// enter a filename in the function by the way of filename
function listFiles (auth, filename =  filename) {
  const drive = google.drive({version: 'v3', auth});
  drive.files.list({
    pageSize: 50,
    fields: 'nextPageToken, files(id, name)',
  }, (err, {data}) => {
    if (err) return console.log('The API returned an error: ' + err);
    const files = data.files;
    if (files.length) {
      console.log('Files:');
      files.map((file) => {
        console.log(`${file.name} (${file.id})`);

            // check if the file returns match the filename wished 
            displayFile(file);
              if(`${file.name}` == filename ){
                console.log("name found !")
                      const fileData = { name : `${file.name}`,
                                         id : `${file.id}`
                      }
         return fileData
        }

      });
    } else {
      console.log('No files found.');
    }
  });
}

listFiles(undefined, "test.md")

任何欢迎改进的想法,

谢谢

推荐答案

在ES2015中添加了默认参数值,您可以为参数声明默认值,并且在进行调用时(如果通过) undefined作为第一个参数,它将获得默认值:

With default parameter values added in ES2015, you can declare default values for the parameters, and when making the call, if you pass undefined as the first parameter, it will get the default:

function test(a = "ay", b = "bee") {
  console.log(`a = ${a}, b = ${b}`);
}
test();             // "a = ay, b = bee"
test(1);            // "a = 1, b = bee"
test(undefined, 2); // "a = ay, b = 2"
test(1, 2);         // "a = 1, b = 2"

您可以在ES2015之前的环境中通过测试undefined手动完成类似的操作:

You can do something similar yourself manually in a pre-ES2015 environment by testing for undefined:

function test(a, b) {
  if (a === undefined) {
    a = "ay";
  }
  if (b === undefined) {
    b = "bee";
  }
  console.log("a = " + a + ", b = " + b);
}
test();             // "a = ay, b = bee"
test(1);            // "a = 1, b = bee"
test(undefined, 2); // "a = ay, b = 2"
test(1, 2);         // "a = 1, b = 2"

这篇关于仅在javascript中传递第二个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆