首页 > 解决方案 > 使用参数名称 (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.
如果您有解决方案,请发布。谢谢。

标签: javascript

解决方案


推荐阅读