node.js - 这个 if 语句究竟做了什么?
问题描述
我正在Discord Bot
使用discord.js
V12 开发,我的代码中有一些我不明白的行。
我的命令处理程序:
const fs = require('fs');
module.exports = (client, Discord) => {
const command_files = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of command_files) {
const command = require(`../commands/${file}`);
//This is the if statement I dont understand
if(command.name)
{
client.commands.set(command.name, command); // and also this line thx.
} else
{
continue;
}
}
};
有人可以向我解释这些吗?谢谢。
解决方案
首先if statement
if(command.name)
检查 的属性是否name
为空。commands
- 如果为空,则**不执行括号内的代码,直接跳转到
else block
- 如果没有,它将执行括号内的代码
第二行
client.commands.set(command.name, command);
调用该.set()
函数以将具有这些参数的新命令添加到commands
集合中:
command.name
显然command
要添加的名称command
具有所有属性和可执行功能的对象本身
推荐阅读
- android - 如果 WebView 遵循替代链接,则返回时会进入无限循环
- python - 如何根据 PID 获取正在运行的 IE 实例
- javascript - phpmyadmin+mysql 实时拉取数据
- java - byte[] 可以转换成连续的输入流
- angular - Ngx-Indexed-db - 数据未保存在 Microsoft Edge 浏览器中,引发数据错误
- python - 如果未找到元素,脚本将不起作用
- c# - 流未打开以供阅读
- php - Sendinblue SMTP 在 CodeIgniter 中不起作用
- regex - jflex正则表达式除零以外的任何数字
- kubernetes - 如何在 Keycloak 启动时添加/修改 LDAP 用户联合