简体   繁体   English

如何确定是否已声明Javascript类?

[英]How to determine if a Javascript class has been declared?

I have some javascript that is being referenced via html that is loaded via AJAX (ie: AJAX retrieves HTML, HTML has a <script> tag in it that references this javascript) 我有一些通过AJAX加载的html被引用的javascript(即:AJAX检索HTML,HTML中有一个<script>标记引用了此javascript)

This javascript contains class declarations, ex: 此javascript包含类声明,例如:

class CalculatorClass{
    ...
}

If this script gets loaded twice, I get an error: redeclaration of let CalculatorClass . 如果此脚本被加载两次,则会出现错误: redeclaration of let CalculatorClass Since it's not a variable, I can't test if it's undefined first - and even then it would restrict the scope. 由于它不是一个变量,因此我无法先测试它是否undefined -即使那样也会限制范围。 How do I only declare the class if it isn't already? 如果尚未声明该类,我该如何声明呢?

您可以使用if (typeof MyClass === 'function')检查是否已声明一个类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM