如何避免在函数内调用函数?

时间:2016-04-18 19:14:45

标签: javascript loops coffeescript

#branchID pool
branch0 = "This is a wall of text. This is another wall of text! This is a third wall of text. This is a fourth wall of text. This is a fifth wall of text. This is a sixth wall of text. #branch1%"  
branch1 = "This is a second wall of text."
branch2 = "This is a third wall of text."
loopcounter = 0

#classes section
  #pulls text from pools above.

branch = (name, branchid)->
  alert('begin loop')
  stringID = String(branchid)
  document.write("<h1 id=\'#{stringID}\'>#{name}</h1>")

  document.getElementById(stringID).onclick = ->
    for i in [loopcounter...stringID.length]
      if branchid[i]!= "." and branchid[i]!="!" and branchid[i]!="?" and branchid[i]!="#"
        document.write(branchid[i])

      else if branchid[i]=="#"
          j = i+1
          for k in [j...stringID.length]
            if branchid[k] == "%"
              j = k+1
              alert("switchblock")
              switch fcode
                when "branch1" then branch('stuff', branch1)
                when "branch2" then branch('stuff2', branch2)
                else break
              break

            else
              alert("gathering...")
              fcode = ""
              fcode += branchid[k]

      else
        alert('end sentence')
        document.write(branchid[i])
        loopcounter = i+1
        break

#This is where the code is executed.
window.onload = ->  
  branch("Start", branch0)

上面的代码是选择你自己的冒险游戏手册的开始。

我的代码通过执行一个函数来工作,该函数一次从长字符串中拉出一个句子并将其写入HTML文档。

我遇到的问题是,当字符串没有留下文本时,我需要再次调用相同的函数,但这次使用不同的参数,以便可以在屏幕上显示不同的字符串。鉴于我目前的情况,我不得不把它称之为自己的功能,但我有一种感觉会导致一些问题。当我尝试运行我的代码时,它的运行方式是我真的不理解和写入文档而不是执行新函数

欢迎任何一般性建议或具体诊断。我此时只是有点难过,不知道从哪里开始。也许我没有正确思考这个问题?顺便说一下,我最近从堆栈溢出中得到了很多帮助。非常感谢。你们一直很棒。

**我扔了一堆警告框,所以我可以试着弄清楚循环在做什么。

Codepen发布:http://codepen.io/bryanwillis7/pen/WwMPaw

2 个答案:

答案 0 :(得分:1)

以下是您尝试做的事情的简化。

现场演示:

https://jsfiddle.net/69r0xq9y/

通常,我建议将数据组织到对象中并以这种方式使用它。字符串解析可能会导致一些不必要的不​​可读代码。

HTML:

<h1 id="name">
  <!-- Branch name inserted here -->
</h1>
<p id="text">
  <!-- Branch text inserted here -->
</p>
<div id="options">
  <!-- Branch options inserted here -->
</div>

CoffeeScript的:

#branchID pool
branches = 
  branch0:
    name: "Start"
    text: "There is a path to a forest and a path to a castle. Where do you want to go?"
    options: 
      branch1: "Forest"
      branch2: "Castle"
  branch1: 
    name: "Forest"
    text: "You are in a forest."
    options:
      branch0: "Go back to start"
  branch2: 
    name: "Castle"
    text: "You are in a castle."
    options:
      branch0: "Go back to start"

#classes section
#pulls text from pools above.
branch = (branchid)->
  document.getElementById('name').innerHTML = branches[branchid].name
  document.getElementById('text').innerHTML = branches[branchid].text
  document.getElementById('options').innerHTML = ''
  for targetBranch,buttonText of branches[branchid].options
    createOption(targetBranch, buttonText)

createOption = (branchid, text) ->
  button = document.createElement('button')
  button.innerHTML = text
  button.onclick = ->
    branch(branchid)
  document.getElementById('options').appendChild(button)

#This is where the code is executed.
window.onload = ->  
  branch("branch0")

答案 1 :(得分:0)

哇,这真是太糟糕了,但我想我已经有了它的工作:

#branchID pool
branch0 = "This is a wall of text. This is another wall of text! This is a third wall of text. This is a fourth wall of text. This is a fifth wall of text. This is a sixth wall of text. #branch1%"  
branch1 = "This is a second wall of text."
branch2 = "This is a third wall of text."


#classes section
  #pulls text from pools above.
branch = (name, branchid)->
  loopcounter = 0
  alert('begin loop')
  stringID = String(branchid)
  document.write("<h1 id=\'#{stringID}\'>#{name}</h1>")
  document.getElementById(stringID).onclick = ->
    for i in [loopcounter...stringID.length]
      if branchid[i]!= "." and branchid[i]!="!" and branchid[i]!="?" and branchid[i]!="#"
        document.write(branchid[i])
      else if branchid[i]=="#"
          j = i+1
          fcode = ""
          for k in [j...stringID.length]
            if branchid[k] == "%"
              j = k+1
              alert("switchblock")
              switch fcode
                when "branch1" then return branch('stuff', branch1)
                when "branch2" then return branch('stuff2', branch2)
                else break
              break
            else
              alert("gathering...")
              fcode += branchid[k]

      else
        alert('end sentence')
        document.write(branchid[i])
        loopcounter = i+1
        break

#This is where the code is executed.
window.onload = ->  
  branch("Start", branch0)

好的,所以我不得不改变一些事情。我将fcode移出switch块并将其放在k循环之前。另外,我在loopcounter函数范围中定义了branch,否则它将不会重置,并且您将遇到index out of bounds问题,这将导致它打印undefined大约一百万次。最后,我在递归调用之前添加了return。这使得父函数停止执行。

老实说,我认为你应该认真考虑将其重构为较小的位。所有这些嵌套循环使得很难跟踪正在发生的事情。尝试制作一个函数来检查这是否是句子的结尾,另一个用于检查你是否正在开始一个新的分支,另一个用于收集字符串。

将代码放在较小的块中也可以使以后更容易测试和更改。

希望有所帮助。