javascript - 不能在另一个脚本中使用脚本中的类
问题描述
我猜这是一个基本问题,但我找不到问题所在。我有两个脚本,一个“主”,一个包含一个基本类,我尝试在主脚本中实例化这个类,如下所示:
脚本.js
window.onload = function() {
var car = new Card('Ressources/Sprites/Axel.png', [50, 50]);
}
Card.js
class Card {
constructor(path, position) {
this.position = position;
this.texture = PIXI.Texture.from(sprite);
this.card = PIXI.Sprite(texture);
this.setPosition(position);
}
function setPosition(position){
card.x = position.x;
card.y = position.y;
}
}
索引.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Hello World</title>
</head>
<style>* {padding: 0; margin: 0}</style>
<script src="Ressources/Libraries/pixi.min.js"></script>
<script src="Scripts/Card.js"></script>
<script src="script.js"></script>
<body>
</body>
</html>
在控制台中我有这个错误:
未捕获的 ReferenceError:卡未在 window.onload 中定义
解决方案
推荐阅读
- flutter-web - flutter web:使用中文字符串时出现乱码怎么解决?
- node.js - 如何运行作为依赖项添加到应用程序中的私有 git 存储库的 npm 命令?
- typescript - 在 react native Expo 中显示上传文件的方式
- python - 在python中计算GCD
- reactjs - 在客户端上为动态页面(使用 CDN 缓存时)执行 SSR 和水合单页应用程序是否有意义?
- bash - CURL 上传文件名中包含 UTF-8 字符(例如 ä、ö、ü)的文件
- nlp - 有哪些方法可以更好地控制 BERT 输出?在多标签文本分类任务中增加/减少某些单词的相关性
- kubernetes - Kubernetes 上的零停机更新。当有上传文件的请求时
- python - 检索预期的 str、字节或 os.PathLike 对象时出现问题,而不是加载图像时出现 dict 错误
- reactjs - openWeatherMap API 调用失败