Javascript OOP,从其他嵌套方法访问方法

时间:2015-04-28 10:44:51

标签: javascript oop methods scope phaser-framework

好的,伙计们,在那里学习我的JS。 我遇到过一个单一的。 这是代码:

hangar = function(game){
}

hangar.prototype = {
  
  loadImages: function(graphicAssets){
    ...
  },
  
  writeTerminal: function(timer, str, destination){
  },
  
  writeStats: function(){
    var writeTerminal = this.writeTerminal;
    console.log('wt', this.writeTerminal);
    console.log('wt2', writeTerminal);
    //ZOMG This does not work!
  },
    
  handleHangarInput: function(layerShips, layerBg){
    
    ... does some variable declarations of which one is:
    
    var writeStats = this.writeStats;
    
    function viewHangarPosition() {
      
      writeStats(); // This works
      
    }
    
    keyleft.onDown.add(function(){
        
        if (currentship > 0) {
            currentship--;
            viewHangarPosition();
        }
    });

    keyright.onDown.add(function(){
        
        if (currentship < shipNumber-1) {
            currentship++;
            viewHangarPosition();
        }
    });

  }
  create: function(){
      this.handleHangarInput(layerShips, layerBg);
  }
    
}

这里是全部调用的地方(index.html):

        window.onload = function() {

            var game = new Phaser.Game(window.innerWidth, window.innerHeight, Phaser.AUTO, '', { preload: preload, create: create, update:update });

            function preload() {
            }

            function create() {
                game.state.add('menu', menu);
                game.state.add('hangar', hangar);
                game.state.start('hangar');
            }

            function update() {
            }
            
        }

我正试图从 writeStats 中获取 writeTerminal ,这对我不起作用。

  • 如何完成
  • 是否有更聪明的方式来使用writeStats而不是在 handleHangarInput 中声明它?

仍然不是关闭和范围的专家。 一如既往地感谢您的帮助!

使用更多代码编辑

以下是修订后的代码:

hangar = function(game){
}

hangar.prototype = {
  
  loadImages: function(graphicAssets){
    ...
  },
  
  writeTerminal: function(timer, str, destination){
  },
  
  writeStats: function(){
      console.log(this); // returns undefined
      console.log(this.writeTerminal); // errors out!
  },
    
  handleHangarInput: function(layerShips, layerBg){
    
    ... does some variable declarations of which one is:
    
    var writeStats = this.writeStats;
    
    function viewHangarPosition() {
      
      writeStats.call(this);
      
    }
    
    keyleft.onDown.add(function(){
        
        if (currentship > 0) {
            currentship--;
            viewHangarPosition();
        }
    });

    keyright.onDown.add(function(){
        
        if (currentship < shipNumber-1) {
            currentship++;
            viewHangarPosition();
        }
    });

  }
  create: function(){
      this.handleHangarInput(layerShips, layerBg);
  }
    
}

2 个答案:

答案 0 :(得分:1)

Javascript具有this关键字的 calltime 绑定。即,this在函数中引用的内容取决于该函数被称为的确切程度(不是如何定义)。简而言之:this指的是调用该方法的对象。

foo.bar();

bar内,关键字this将引用foo

bar();

bar内,关键字this不会特别引用任何内容,因此默认为全局对象window

var bar = foo.bar;
bar();

与上面(window)相同,因为函数的调用方式如何。

如果您想重新分配对象方法但仍然在调用时保持其上下文,则需要明确处理:

var bar = foo.bar.bind(foo);
bar();

// or:
var bar = foo.bar();
bar.call(foo);

答案 1 :(得分:0)

不确定这是你的要求,但是当你打电话时:

writeStats(); 

在viewHangarPosition中完成,因为您没有将该函数应用于任何对象,所以它是作为this对象传递给writeStats()的窗口对象。所以在writeStats中,this.writeTerminal将是未定义的。

相关问题