我有这个非常简单的 html 文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>
<body>
<script src="https://code.jquery.com/jquery.js"></script>
<script src="js/app.js"></script>
<script src="js/model/dinnerModel.js"></script>
<script src="js/view/exampleView.js"></script>
</body>
</html>
底部包含的 javascript 文件是:
app.js:
$(function() {
var model = new DinnerModel();
var exampleView = new ExampleView($("#exampleView"));
});
晚餐模型.js:
var DinnerModel = function(){
// some js stuff
}
exampleView.js:
var ExampleView = function () {
// more js stuff
}
这对我来说运行良好,我的问题是:为什么?当 app.js 包含在其脚本标记中时,dinnermodel.js 和 exampleView.js 显然还没有加载,所以我应该在 app.js 中收到一个错误,指出 DiningModel 未声明,对吗?
最佳答案
因为 $(function() {})
会等待 dom 准备好,这意味着其他脚本在其中的代码执行之前已加载
关于javascript - 当其他文件引用的变量尚未加载时,为什么 javascript 不会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48465573/