在 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 = 谷歌.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"
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tangegcfc
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
检查邮件是否发送成功
it1352 07-25 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20