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

标签 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

最佳答案

这是您尝试执行的操作的简化。

现场演示:

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")

关于javascript - 如何避免在函数内调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36702486/

相关文章:

javascript - 如何按值传递到 javascript Promise

javascript - 将 javascript 变量发送到经典的 asp

javascript - 提交时文本区域验证

perl - 我们可以在 Perl 中同时运行两个非嵌套循环吗?

c - for、while 和 do while 循环之间的性能差异?

php - 如何使用 Laravel 在主 CoffeeScript 中需要其他 CoffeeScripts 文件?

javascript - 从 CoffeeScript 调用 JavaScript "new"

javascript - 如何让 Observable.flatMap 等待解析

php - 在 php 中循环 - 创建不同的卷号

javascript - Canvas 上绘制的图形出现一小会儿,然后就消失了