[英]How to set class and export correctly in javascript and Node.js
I use node.js
and I loaded js
file in ejs
我使用
node.js
并在ejs
中加载了js
文件
When main.js
will be loaded,script will be functioned.加载
main.js
时,脚本将运行。 while then,那时,
<script type="text/javascript" src=../javascripts/functions.js></script>
<script type="text/javascript" src=../javascripts/quiz.js></script>
<script type="text/javascript" src=../javascripts/main.js></script>
I have some issues like Uncaught ReferenceError: module is not defined
in quiz.js
我有一些问题,例如
Uncaught ReferenceError: module is not defined
in quiz.js
How to set export
correctly?如何正确设置
export
?
I'd like to export quiz
class correctly.我想正确导出
quiz
class。 Is this issue comes from ES6
?这个问题来自
ES6
吗?
If someone has opinion,please let me know.如果有人有意见,请告诉我。
Thanks谢谢
quiz.js
class Quiz {
constructor(quizData){
this._quizzes = quizData.results;
this._correctAnswersNum = 0;
}
getQuestion(index){
return this._quizzes[index-1].question;
}
}
module.exports = Quiz;
main.js
(()=>{
const url = "/quiz-data";
fetch(url)
.then(response => response.json())
.then(json => {
const quiz = new Quiz(json);
console.log("json",json);
console.log("quiz",quiz);
displayQuiz(quiz,1);
});
})();
Client-side JS does not support Node.js style CommonJS modules .客户端 JS 不支持 Node.js 风格的CommonJS 模块。
If you want to use modules client-side, then write ES6 modules .如果您想在客户端使用模块,请编写ES6 modules 。
If you just want to use shared globals, then write the code as you have it now only without the module.exports
line.如果您只想使用共享的全局变量,那么只需在没有
module.exports
行的情况下编写您现在拥有的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.