Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
945 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 Answer

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...