首页 > 解决方案 > 为什么javascript在控制台中显示“Uncaught SyntaxError: Identifier 'Common' has already been declared after making javascript class object”?

问题描述

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>

标签: javascriptphpjquerylaravel

解决方案


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

<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>


推荐阅读