javascript - 使用参数名称 (JavaScript) 从函数构造类
问题描述
我正在尝试使用函数的参数之一从函数构造一个类:id
但代码认为我正在尝试将参数设置为类。这是我的代码:
function assureVote() {
if(confirm("Are you sure you want to submit your vote?")) {
submitVote();
}
}
function submitVote(id = document.getElementById("id"), party = document.getElementById("party")) {
const id = new SubmittedVote(party.value);
}
class SubmittedVote {
constructor(party) {
this._party = party;
}
}
<!DOCTYPE html>
<html>
<head>
<script src="js.js">
</script>
</head>
<body>
<input id="id" autocomplete="off" type="password">
<select name="Party" id="party">
<option value="mr">Melon Republic</option>
<option value="dd">DD2021</option>
</select>
<button onclick="assureVote()">Submit vote</button>
</body>
</html>
问题是在submitVote
函数中,我有const id = new SubmittedVote(party.value);
,并且 JavaScript 认为我正在尝试将id
参数声明为类,它返回错误,因为我使用了const
.
如果您有解决方案,请发布。谢谢。
解决方案
推荐阅读
- stripe-payments - 配置 Stripe 订阅
- c# - 使用正则表达式匹配 C# 搜索字符串模式
- crystal-reports - Crystal Reprots 2013 迁移到新服务器
- ffmpeg - 带 Alpha 通道的自适应视频编码
- r - 将多个excel文件作为一个数据框/表读取
- c# - Angular 9 http.delete 返回不支持的媒体 415
- c# - 使用 HoloLens 2 获取空间坐标系的正确方法
- android - 在 Room Kotlin 中插入改造响应
- asp.net-core - 如何在 ASP.Net Core 应用程序中向 Azure AD 身份验证用户添加自定义声明?
- android - 在 Android Studio 中运行单元测试时,类覆盖率和行覆盖率有什么区别?