Issue
I tried to define an "enum" using an Object and freeze inside a JS
class in the next manner:
//test.js
class MyClass{
static MyEnum = Object.freeze({
kZero: 0,
kOne: 1
});
}
console.log(MyClass.MyEnum.kZero);
console.log(MyClass.MyEnum.kOne);
When running the html
file in firefox
I get the next output:
0 test.js:8:9
1 test.js:9:9
Uncaught SyntaxError: redeclaration of let MyClass
<anonymous> file:<path>/test.js:1
Can someone please explain what's wrong or direct me to a source where it's explained? I read the related posts, but all I could find are cases when let
is indeed used.
The html
file:
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<script src="test.js" charset="utf-8"></script>
<link rel="stylesheet" href="test_styles.css"></link>
<title>Page</title>
</head>
<body>
<script src="test.js"></script>
</body>
</html>
and the css
file it empty.
Solution
It looks like you have the script tag for "test.js" included twice, once in the head and once in the body of your HTML file. This is causing the script to be loaded and executed twice, which results in the error you're seeing.
Here's the corrected HTML file:
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<script src="test.js" charset="utf-8"></script>
<link rel="stylesheet" href="test_styles.css"></link>
<title>Page</title>
</head>
<body>
<!-- Remove the following line -->
<!-- <script src="test.js"></script> -->
</body>
</html>
By removing the extra script tag in the body, you should only load "test.js" once, and the redeclaration error should be resolved. Make sure to clear your browser cache before testing the updated HTML file.
Answered By - Kishan Padsala
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.