【问题标题】:Calling functions from within functions JS从函数 JS 中调用函数
【发布时间】:2016-06-11 08:18:50
【问题描述】:

我正在编写一个小型战舰游戏,并且我正在使用我不太熟悉的 Javascript 对象重构我的代码。我希望能够使用对象从函数中调用函数,但我似乎无法弄清楚如何做到这一点。代码在这里:

<script>
var xPos;
var yPos;
var boatGrid = {

    selectPos : function() {
        console.log("it works"); //want to try calling function addNumber() here
        for (boatNum = 1; boatNum < 4; boatNum++) {
            xPos = Math.floor(Math.random() * 8);
            yPos = Math.floor(Math.random() * 10 + 1);
        }
    },
    buildBoat : function() {
        console.log("this works too");
        for (boatLen = 1; boatLen < 4; boatLen++) {
            xPos = xPos++;
            boatPos = "cell_" + xPos + "_" + yPos;
        }
    },
    addNumber : function() {
        document.getElementById("test2").innerHTML = "hello"; //debug line
    }
}

addNum() 函数作为调试存在。

【问题讨论】:

  • 我听不懂。你的意思是打电话给boatGrid.addNumber() 吗?

标签: javascript object


【解决方案1】:

你快到了。

在您的情况下,boatGrid 是一个对象,addNumber 是它的方法之一。所以使用this.addNumber()

var xPos;
var yPos;
var boatGrid = {

    selectPos : function() {
        console.log("it works"); //want to try calling function addNumber() here
     this.addNumber();   // Changed here
     for (boatNum = 1; boatNum < 4; boatNum++) {
            xPos = Math.floor(Math.random() * 8);
            yPos = Math.floor(Math.random() * 10 + 1);
        }
    },
    buildBoat : function() {
        console.log("this works too");
        for (boatLen = 1; boatLen < 4; boatLen++) {
            xPos = xPos++;
            boatPos = "cell_" + xPos + "_" + yPos;
        }
    },
    addNumber : function() {
        //document.getElementById("test2").innerHTML = "hello"; //debug line
   document.write('<pre>Add Number called</pre>')

    }
}
boatGrid.selectPos();

jsfiddle

【讨论】:

    【解决方案2】:

    使用

    this.addNumber();
    

    您的代码应如下所示

    selectPos : function() {
        console.log("it works");
        this.addNumber(); //want to try calling function addNumber() here
        for (boatNum = 1; boatNum < 4; boatNum++) {
            xPos = Math.floor(Math.random() * 8);
            yPos = Math.floor(Math.random() * 10 + 1);
        }
    },
    

    【讨论】:

      【解决方案3】:

      如果您想调用boatGrid.selectPos() 并在selectPos 函数中调用addNumber 函数,只需在selectPos 函数中添加this.addNumber();

          selectPos : function() {
              console.log("it works");
              this.addNumber(); // <---- boatGrid calls addNumber()
              for (boatNum = 1; boatNum < 4; boatNum++) {
                  xPos = Math.floor(Math.random() * 8);
                  yPos = Math.floor(Math.random() * 10 + 1);
              }
          }
      

      在对象函数中使用时,this 指的是调用该函数的对象。

      以我的原始示例为例,boatGrid 调用selectPos,所以selectPos 函数中的this 指的是boatGrid

      那么this.addNumber() 将让boatGrid 对象调用它的addNumber 函数。

      【讨论】:

        猜你喜欢
        • 2018-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-14
        • 2020-02-29
        • 1970-01-01
        • 2013-05-28
        相关资源
        最近更新 更多