<bdo id='0dBn0'></bdo><ul id='0dBn0'></ul>
      <legend id='0dBn0'><style id='0dBn0'><dir id='0dBn0'><q id='0dBn0'></q></dir></style></legend>
    1. <tfoot id='0dBn0'></tfoot>
    2. <i id='0dBn0'><tr id='0dBn0'><dt id='0dBn0'><q id='0dBn0'><span id='0dBn0'><b id='0dBn0'><form id='0dBn0'><ins id='0dBn0'></ins><ul id='0dBn0'></ul><sub id='0dBn0'></sub></form><legend id='0dBn0'></legend><bdo id='0dBn0'><pre id='0dBn0'><center id='0dBn0'></center></pre></bdo></b><th id='0dBn0'></th></span></q></dt></tr></i><div id='0dBn0'><tfoot id='0dBn0'></tfoot><dl id='0dBn0'><fieldset id='0dBn0'></fieldset></dl></div>

      <small id='0dBn0'></small><noframes id='0dBn0'>

    3. 在 javascript 中只传递第二个参数

      时间:2023-10-21
      • <tfoot id='ubsge'></tfoot>
        1. <small id='ubsge'></small><noframes id='ubsge'>

          • <bdo id='ubsge'></bdo><ul id='ubsge'></ul>

            1. <legend id='ubsge'><style id='ubsge'><dir id='ubsge'><q id='ubsge'></q></dir></style></legend>
                <i id='ubsge'><tr id='ubsge'><dt id='ubsge'><q id='ubsge'><span id='ubsge'><b id='ubsge'><form id='ubsge'><ins id='ubsge'></ins><ul id='ubsge'></ul><sub id='ubsge'></sub></form><legend id='ubsge'></legend><bdo id='ubsge'><pre id='ubsge'><center id='ubsge'></center></pre></bdo></b><th id='ubsge'></th></span></q></dt></tr></i><div id='ubsge'><tfoot id='ubsge'></tfoot><dl id='ubsge'><fieldset id='ubsge'></fieldset></dl></div>
                  <tbody id='ubsge'></tbody>

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

                问题描述

                我正在尝试创建一个函数,其中我只传递函数的 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 = 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")
                

                欢迎提出任何改进意见.

                Any improving ideas are welcome.

                推荐答案

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

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

                上一篇:Javascript:参数数组是否已弃用? 下一篇:为什么数组切片将javascript参数转换为数组

                相关文章

                <small id='FISOD'></small><noframes id='FISOD'>

                  <tfoot id='FISOD'></tfoot>
                  • <bdo id='FISOD'></bdo><ul id='FISOD'></ul>

                    <i id='FISOD'><tr id='FISOD'><dt id='FISOD'><q id='FISOD'><span id='FISOD'><b id='FISOD'><form id='FISOD'><ins id='FISOD'></ins><ul id='FISOD'></ul><sub id='FISOD'></sub></form><legend id='FISOD'></legend><bdo id='FISOD'><pre id='FISOD'><center id='FISOD'></center></pre></bdo></b><th id='FISOD'></th></span></q></dt></tr></i><div id='FISOD'><tfoot id='FISOD'></tfoot><dl id='FISOD'><fieldset id='FISOD'></fieldset></dl></div>
                  1. <legend id='FISOD'><style id='FISOD'><dir id='FISOD'><q id='FISOD'></q></dir></style></legend>