我有几个用以下结构构造的javascript对象。我不知道这是否正确或结构是否不可推荐,但我想知道如何从指定地点调用该方法:
function Graphic_Interface (){
var btn = document.querySelector(".btn");
btn.addEventListener("click", function(){
//I want to call game > obj > doSomething() from here; how can I do it?
})
}
function Another_Object(){
this.doSomething = function(){
console.log('doing something');
};
}
function Game (){
var gi = new Graphic_Interface();
var obj = new Another_Object();
}
var game = new Game();
有可能吗?施工权对吗?有更好的方法吗?
答案 0 :(得分:0)
如果我理解正确(假设Game
用作构造函数),您希望将Game
对象传递给Graphic_Interface
。
像
这样的东西function Graphic_Interface (game){
var btn = document.querySelector(".btn");
btn.addEventListener("click", function(){
game.obj.do_something();
})
}
function Another_Object(){
this.doSomething = function(){
console.log('doing something');
};
}
function Game (){
var gi = new Graphic_Interface(this);
this.obj = new Another_Object(); // Note saving as member
}