gpt4 book ai didi

javascript - 为什么 javascript 显示 "Uncaught SyntaxError: Identifier ' Common' has already been declared after making javascript class object"in console?

转载 作者:行者123 更新时间:2023-11-30 06:13:11 28 4
gpt4 key购买 nike

class Common
{
constructor(){
this.URL=location.protocol + '//' + location.host+'';
}

showOk(){
console.log('ok');
}

}
const commn=new Common();

这是我尝试调用 showOk() 函数的 html 代码。

<button type="button" class="btn btn-01 btn-checkout-login ml-auto w-100 text-center" onclick="return commn.showOk();" ;="">Continue Checkout</button>

<button type="button" class="btn btn-01 btn-checkout-login ml-auto w-100 text-center" onclick="commn.showOk();" ;="">Continue Checkout</button>

最佳答案

你可以这样写,你要知道,类的实例的功能和普通的功能是不一样的。

<button onclick="handleClick();">click</button>

<script >

class Common {
constructor(){
this.URL=location.protocol + '//' + location.host+'';
}

showOk(){
console.log('ok');
}

}
const common = new Common();

function handleClick() {
common.showOk();
}
</script>

关于javascript - 为什么 javascript 显示 "Uncaught SyntaxError: Identifier ' Common' has already been declared after making javascript class object"in console?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57667000/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com