javascript - 为什么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>
解决方案
你可以这样写,你需要知道,类的实例的功能和普通的功能是不同的。
<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 - 域名过期或网站不工作时如何访问谷歌索引的内容
- wordpress - 菜单仅在 WordPress 中未显示在移动主页上
- postgresql - 如何横向加入函数
- blockchain - 在 PoW 网络中,当强大的矿工在场时,弱矿工如何获得奖励?
- asp.net-core - 将 Google AMP 应用于 MVC Core 应用程序(替代 MVC Core 的显示模式)
- django - Django:模拟模型的创建方法
- javascript - 如何通过两个相同的键按json数组分组
- machine-learning - 使用 word2vec 词嵌入作为特征向量进行文本分类(类似于计数向量器/tfidf 特征向量)
- dialogflow-es - 从单个单词中提取 2 个实体:对话流
- c++ - 带有 pthread 的 Makefile 无法在多个版本中正确编译