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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…