• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在 javascript 只传递第二个参数

用户头像
it1352
帮助6

问题说明

我正在尝试创建一个函数,其中我只传递函数的 second 参数.

I'm trying to make a function in which I pass only the second argument of my function.

我希望它以这种方式工作:

I would like it to work this way:

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

我目前的想法是将第二个参数作为 de facto 动态默认参数传递如下:

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 using the Google drive API, and I'm trying to make it such that I can enter a string value for the second parameter to make a callback. This callback would take the role of verifying whether the return file is effectively the file searched (by making a boolean verification on name value).

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

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

我希望这种精确度会有用.

I hope this precision will be useful.

这是我的 quickstart.js :

Here is 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 = 谷歌.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")

欢迎提出任何改进意见.

Any improving ideas are welcome.

正确答案

#1

在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"

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

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"

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tangegcfc
系列文章
更多 icon
同类精品
更多 icon
继续加载