好吧..所以我想明白了。但我想确保它不可能。
假设我有 3 个文件:
index.js
主文件,player.js
播放器类文件,game.js
游戏类文件。
主文件(索引)正在导入所有类。但是,我需要有在两个文件中创建游戏对象的选项
例子:
index.js
:
var game = require("./game.js");
var player = require("./player.js");
new player("player");
new game("game");
player.js
:
module.exports = class Player {
constructor(arg){
console.log(arg);
var asd=new game("ASD");/// <- the problem
}
};
game.js
:
module.exports = class Game {
constructor(arg){
console.log(arg);
}
};
如何在 index.js
和 player.js
中调用 player 类中的 Game 而不需要游戏
我知道这会解决它,但是还有没有重复要求的另一种方法吗?
(2 个要求,一个在 index.js
中,一个在 player.js
中)
最佳答案
index.js:
var player = require("./player.js");
var game = player.Game;
new player("player");
new game("game");
播放器.js:
var game = require("./game.js");
module.exports = class Player {
constructor(arg){
console.log(arg);
var asd=new game("ASD");/// <- the problem
}
};
modules.exports.Game = Game;
游戏.js:
module.exports = class Game {
constructor(arg){
console.log(arg);
}
};
关于javascript - 导入类之间的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54111548/