Javascript类访问和单一入口点?
嗨,我是来自C#背景的Javascript新手。关于如何从一个类访问另一个类,我有一个小问题 我有一个入口点javascript文件(html中的script标记中包含Main.js)和两个javascript类(未通过script标记包含)Javascript类访问和单一入口点?,javascript,oop,Javascript,Oop,嗨,我是来自C#背景的Javascript新手。关于如何从一个类访问另一个类,我有一个小问题 我有一个入口点javascript文件(html中的script标记中包含Main.js)和两个javascript类(未通过script标记包含) 游戏世界 玩家 我想在Main.js中使用GameWord的对象,比如 function init(); { this.gameWorld = new GameWorld(); } init(); 类似地,我想在GameWorld类中使用P
- 游戏世界
- 玩家
function init();
{
this.gameWorld = new GameWorld();
}
init();
类似地,我想在GameWorld类中使用Player类的对象,比如
class GameWorld
{
constructor()
{
this.player = new Player();
}
}
GameWorld类使用一个库“pixi.js”。是否有一种简单的方法来创建对象并包含库?
另外,我希望在我的HTML脚本中只有一个入口点,即Main.js文件,而不是将它们添加到HTML索引页面的脚本标记中
谢谢,希望得到肯定的答复。西娅 您可以使用javascript模块系统 GameWorld.js 它将导入Player.js
import Player from "./Player"
//this is relative path.It may change according to your project structure
export default class GameWorld {
static testGameMethod = function () {
console.log("I am from game class will call player");
Player.testPlayerMethod()
}
}
Player.js
import Player from "./Player"
//this is relative path.It may change according to your project structure
export default class GameWorld {
static testGameMethod = function () {
console.log("I am from game class will call player");
Player.testPlayerMethod()
}
}
export default class Players {
static testPlayerMethod = function () {
console.log("I am from player class")
}
}
然后在main.js中导入GameWorld.js
import GameWorld from "./GameWorld";
GameWorld.testGameMethod()
这是一个正在工作的。请检查控制台以查看输出