Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
963 views
in Technique[技术] by (71.8m points)

function - Pass only the second argument in javascript

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

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) {
    // ...
}

...then change the defaultVar value according to my needs.

var defaultVar = modification; 

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

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...