javascript - Calling functions from within functions JS -
i'm writing small battleships game, , i'm refactoring code using javascript objects, tool not familiar with. able call function within function using objects, , cannot seem work out how this. code here:
<script> var xpos; var ypos; var boatgrid = { selectpos : function() { console.log("it works"); //want try calling function addnumber() here (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"); (boatlen = 1; boatlen < 4; boatlen++) { xpos = xpos++; boatpos = "cell_" + xpos + "_" + ypos; } }, addnumber : function() { document.getelementbyid("test2").innerhtml = "hello"; //debug line } }
the addnum() function there debug.
you there.
in case boatgrid
object , addnumber
1 of it's method. use this.addnumber()
var xpos; var ypos; var boatgrid = { selectpos : function() { console.log("it works"); //want try calling function addnumber() here this.addnumber(); // changed here (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"); (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();
Comments
Post a Comment