0

我有一个 Coffeescript 类,我想调用 jQuery 函数,但它们似乎无法访问 DOM。

我的代码是这样的:

$(document).ready ->
  $("#start-button").on "click", ->
    game = new Gameplay()
    game.playInning()

class @Gameplay

  ... most omitted

  # display
  displayAddBall: (balls) ->
    $("#gameplay-at-bat-ball-indicator#{balls}").removeClass("glyphicon glyphicon-unchecked")
    $("#gameplay-at-bat-ball-indicator#{balls}").addClass("glyphicon glyphicon-check")

  displayAddStrike: (strikes) ->
    $("#gameplay-at-bat-strike-indicator#{strikes}").removeClass("glyphicon glyphicon-unchecked")
    $("#gameplay-at-bat-strike-indicator#{strikes}").addClass("glyphicon glyphicon-check")

如果我在 中调用这些函数document(ready),就像gameplay.displayAddBall它工作正常一样,但是当我在类中调用相同的函数时,它不起作用并且没有给出错误。我的想法是它需要对 DOM 的引用,并且在类中没有它。我需要通过它,但我不确定它的机制。

这是完整的代码(我试图包含已编译的 JS 代码,但它太大了):

$(document).ready ->
  $("#start-button").on "click", ->
    game = new Gameplay()
    game.playInning()

class @Gameplay
  PITCH_BALL_PROB = 50
  PITCH_STRIKE_PROB = 35
  PITCH_CONTACT_PROB = 100 - PITCH_BALL_PROB - PITCH_STRIKE_PROB

  PITCH_BALL_RANGE = [0, PITCH_BALL_PROB]
  PITCH_STRIKE_RANGE = [(PITCH_BALL_PROB) + 1, PITCH_BALL_PROB + PITCH_STRIKE_PROB]
  PITCH_CONTACT_RANGE = [(PITCH_BALL_PROB + PITCH_STRIKE_PROB) + 1, 100]

  CONTACT_FOUL_PROB = 35
  CONTACT_SINGLE_PROB = 15
  CONTACT_DOUBLE_PROB = 5
  CONTACT_TRIPLE_PROB = 1
  CONTACT_HOME_RUN_PROB = 4
  CONTACT_POP_FLY_OUT_PROB = 20
  CONTACT_GROUND_BALL_OUT_PROB = 20

  CONTACT_FOUL_RANGE = [0, CONTACT_FOUL_PROB]
  CONTACT_SINGLE_RANGE = [CONTACT_FOUL_PROB + 1, CONTACT_FOUL_PROB + CONTACT_SINGLE_PROB]
  CONTACT_DOUBLE_RANGE = [CONTACT_FOUL_PROB + CONTACT_SINGLE_PROB + 1, CONTACT_FOUL_PROB + CONTACT_SINGLE_PROB + CONTACT_DOUBLE_PROB]
  CONTACT_TRIPLE_RANGE = [CONTACT_FOUL_PROB + CONTACT_SINGLE_PROB + CONTACT_DOUBLE_PROB + 1, CONTACT_FOUL_PROB + CONTACT_SINGLE_PROB + CONTACT_DOUBLE_PROB + CONTACT_TRIPLE_PROB]
  CONTACT_HOME_RUN_RANGE = [CONTACT_FOUL_PROB + CONTACT_SINGLE_PROB + CONTACT_DOUBLE_PROB + CONTACT_TRIPLE_PROB + 1, CONTACT_FOUL_PROB + CONTACT_SINGLE_PROB + CONTACT_DOUBLE_PROB + CONTACT_TRIPLE_PROB + CONTACT_HOME_RUN_PROB]
  CONTACT_POP_FLY_OUT_RANGE = [CONTACT_FOUL_PROB + CONTACT_SINGLE_PROB + CONTACT_DOUBLE_PROB + CONTACT_TRIPLE_PROB + CONTACT_HOME_RUN_PROB, CONTACT_FOUL_PROB + CONTACT_SINGLE_PROB + CONTACT_DOUBLE_PROB + CONTACT_TRIPLE_PROB + CONTACT_HOME_RUN_PROB + CONTACT_POP_FLY_OUT_PROB]
  CONTACT_GROUND_BALL_OUT_RANGE = [CONTACT_FOUL_PROB + CONTACT_SINGLE_PROB + CONTACT_DOUBLE_PROB + CONTACT_TRIPLE_PROB + CONTACT_HOME_RUN_PROB + CONTACT_POP_FLY_OUT_PROB + 1, 100]


  # Pitch Results
  pitch: ->
    Math.floor (Math.random() * 100) + 1

  pitchResult: (pitch) ->
    if @inBallRange(pitch) then return "ball"
    if @inStrikeRange(pitch) then return "strike"
    if @inContactRange(pitch) then return "contact"

  inBallRange: (pitch) ->
    pitch > PITCH_BALL_RANGE[0] && pitch <= PITCH_BALL_RANGE[1]

  inStrikeRange: (pitch) ->
    pitch > PITCH_STRIKE_RANGE[0] && pitch <= PITCH_STRIKE_RANGE[1]

  inContactRange: (pitch) ->
    pitch > PITCH_CONTACT_RANGE[0] && pitch <= PITCH_CONTACT_RANGE[1]


  # Contact results
  contact: ->
    Math.floor (Math.random() * 100) + 1

  contactResult: (contact) ->
    if @inFoulRange(contact) then return "foul"
    if @inSingleRange(contact) then return "single"
    if @inDoubleRange(contact) then return "double"
    if @inTripleRange(contact) then return "triple"
    if @inHomeRunRange(contact) then return "home run"
    if @inPopFlyOutRange(contact) then return "pop fly out"
    if @inGroundBallOutRange(contact) then return "ground ball out"

  inFoulRange: (contact) ->
    contact >= CONTACT_FOUL_RANGE[0] && contact <= CONTACT_FOUL_RANGE[1]

  inSingleRange: (contact) ->
    contact >= CONTACT_SINGLE_RANGE[0] && contact <= CONTACT_SINGLE_RANGE[1]

  inDoubleRange: (contact) ->
    contact >= CONTACT_DOUBLE_RANGE[0] && contact <= CONTACT_DOUBLE_RANGE[1]

  inTripleRange: (contact) ->
    contact >= CONTACT_TRIPLE_RANGE[0] && contact <= CONTACT_TRIPLE_RANGE[1]

  inHomeRunRange: (contact) ->
    contact >= CONTACT_HOME_RUN_RANGE[0] && contact <= CONTACT_HOME_RUN_RANGE[1]

  inPopFlyOutRange: (contact) ->
    contact >= CONTACT_POP_FLY_OUT_RANGE[0] && contact <= CONTACT_POP_FLY_OUT_RANGE[1]

  inGroundBallOutRange: (contact) ->
    contact >= CONTACT_GROUND_BALL_OUT_RANGE[0] && contact <= CONTACT_GROUND_BALL_OUT_RANGE[1]

  # At-bat
  atbat: ->
    balls = 0
    strikes = 0
    contact = null
    atbat = true

    while atbat
      result = @pitchResult(@pitch())
      @displayAddGameReport("@atbat: Pitch Result -> result = #{result}")
      switch result
        when "ball" then balls = @ballReceived(balls)
        when "strike" then strikes = @strikeReceived(contact, strikes)
        when "contact" then contact = @contactReceived()

      if balls is 4
        result = "walk"
        @displayAddGameReport("Walk")
        atbat = false

      if strikes is 3
        result = "strikeout"
        atbat = false

      if contact is "foul"
        strikes = @strikeReceived(contact, strikes)

      if contact? and contact isnt "foul"
        result = contact
        atbat = false
      contact = null

      @displayAddGameReport("@atbat: balls = #{balls}, strikes = #{strikes}, contact = #{contact}")

    return result

  atBatResult: (result, score, baseOccupancy) ->
    switch result
      when "strikeout" then @displayAddGameReport("StrikeOut")
      when "pop fly out" then @displayAddGameReport("Pop fly out")
      when "ground ball out" then @displayAddGameReport("Ground ball out")
      else
        score = @updateBaseOccupancy(baseOccupancy, result, score)
    return { result: result, score: score }

  ballReceived: (balls) ->
    @displayAddBall(balls + 1)
    return balls + 1

  strikeReceived: (contact, strikes) ->
    if contact is "foul" and strikes is 2
      return strikes
    @displayAddStrike(strikes + 1)
    return strikes + 1

  contactReceived: ->
    result = @contactResult(@contact())
    @displayAddGameReport(result) #unless result is "foul"
    return result

  updateBaseOccupancy: (baseOccupancy, result, score) ->
    if result is "walk"
      if baseOccupancy.first is "manned" and baseOccupancy.second is "manned" and baseOccupancy.third is "manned"
        score += 1
        baseOccupancy.first = "manned"
        baseOccupancy.second = "manned"
        baseOccupancy.third = "manned"
      else if baseOccupancy.first is "manned" and baseOccupancy.second is "manned" and baseOccupancy.third is "empty"
        baseOccupancy.first = "manned"
        baseOccupancy.second = "manned"
        baseOccupancy.third = "manned"
      else if baseOccupancy.first is "empty" and baseOccupancy.second is "manned" and baseOccupancy.third is "manned"
        baseOccupancy.first = "manned"
        baseOccupancy.second = "manned"
        baseOccupancy.third = "manned"
      else if baseOccupancy.first is "manned" and baseOccupancy.second is "empty" and baseOccupancy.third is "manned"
        baseOccupancy.first = "manned"
        baseOccupancy.second = "manned"
        baseOccupancy.third = "manned"
      else if baseOccupancy.first is "manned" and baseOccupancy.second is "empty" and baseOccupancy.third is "empty"
        baseOccupancy.first = "manned"
        baseOccupancy.second = "manned"
        baseOccupancy.third = "empty"
      else if baseOccupancy.first is "empty" and baseOccupancy.second is "manned" and baseOccupancy.third is "empty"
        baseOccupancy.first = "manned"
        baseOccupancy.second = "manned"
        baseOccupancy.third = "empty"
      else if baseOccupancy.first is "empty" and baseOccupancy.second is "empty" and baseOccupancy.third is "manned"
        baseOccupancy.first = "manned"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "manned"
      else if baseOccupancy.first is "empty" and baseOccupancy.second is "empty" and baseOccupancy.third is "empty"
        baseOccupancy.first = "manned"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "empty"
    if result is "single"
      if baseOccupancy.first is "manned" and baseOccupancy.second is "manned" and baseOccupancy.third is "manned"
        score += 1
        baseOccupancy.first = "manned"
        baseOccupancy.second = "manned"
        baseOccupancy.third = "manned"
      else if baseOccupancy.first is "manned" and baseOccupancy.second is "manned" and baseOccupancy.third is "empty"
        baseOccupancy.first = "manned"
        baseOccupancy.second = "manned"
        baseOccupancy.third = "manned"
      else if baseOccupancy.first is "empty" and baseOccupancy.second is "manned" and baseOccupancy.third is "manned"
        score += 1
        baseOccupancy.first = "manned"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "manned"
      else if baseOccupancy.first is "manned" and baseOccupancy.second is "empty" and baseOccupancy.third is "manned"
        score += 1
        baseOccupancy.first = "manned"
        baseOccupancy.second = "manned"
        baseOccupancy.third = "empty"
      else if baseOccupancy.first is "manned" and baseOccupancy.second is "empty" and baseOccupancy.third is "empty"
        baseOccupancy.first = "manned"
        baseOccupancy.second = "manned"
        baseOccupancy.third = "empty"
      else if baseOccupancy.first is "empty" and baseOccupancy.second is "manned" and baseOccupancy.third is "empty"
        baseOccupancy.first = "manned"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "manned"
      else if baseOccupancy.first is "empty" and baseOccupancy.second is "empty" and baseOccupancy.third is "manned"
        score += 1
        baseOccupancy.first = "manned"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "empty"
      else if baseOccupancy.first is "empty" and baseOccupancy.second is "empty" and baseOccupancy.third is "empty"
        baseOccupancy.first = "manned"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "empty"
    if result is "double"
      if baseOccupancy.first is "manned" and baseOccupancy.second is "manned" and baseOccupancy.third is "manned"
        score += 2
        baseOccupancy.first = "empty"
        baseOccupancy.second = "manned"
        baseOccupancy.third = "manned"
      else if baseOccupancy.first is "manned" and baseOccupancy.second is "manned" and baseOccupancy.third is "empty"
        score += 1
        baseOccupancy.first = "empty"
        baseOccupancy.second = "manned"
        baseOccupancy.third = "manned"
      else if baseOccupancy.first is "empty" and baseOccupancy.second is "manned" and baseOccupancy.third is "manned"
        score += 2
        baseOccupancy.first = "empty"
        baseOccupancy.second = "manned"
        baseOccupancy.third = "empty"
      else if baseOccupancy.first is "manned" and baseOccupancy.second is "empty" and baseOccupancy.third is "manned"
        score += 1
        baseOccupancy.first = "empty"
        baseOccupancy.second = "manned"
        baseOccupancy.third = "manned"
      else if baseOccupancy.first is "empty" and baseOccupancy.second is "manned" and baseOccupancy.third is "empty"
        score += 1
        baseOccupancy.first = "empty"
        baseOccupancy.second = "manned"
        baseOccupancy.third = "empty"
      else if baseOccupancy.first is "empty" and baseOccupancy.second is "empty" and baseOccupancy.third is "manned"
        score += 1
        baseOccupancy.first = "empty"
        baseOccupancy.second = "manned"
        baseOccupancy.third = "empty"
      else if baseOccupancy.first is "manned" and baseOccupancy.second is "empty" and baseOccupancy.third is "empty"
        baseOccupancy.first = "empty"
        baseOccupancy.second = "manned"
        baseOccupancy.third = "manned"
      else if baseOccupancy.first is "empty" and baseOccupancy.second is "empty" and baseOccupancy.third is "empty"
        baseOccupancy.first = "empty"
        baseOccupancy.second = "manned"
        baseOccupancy.third = "empty"
    if result is "triple"
      if baseOccupancy.first is "manned" and baseOccupancy.second is "manned" and baseOccupancy.third is "manned"
        score += 3
        baseOccupancy.first = "empty"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "manned"
      else if baseOccupancy.first is "empty" and baseOccupancy.second is "manned" and baseOccupancy.third is "manned"
        score += 2
        baseOccupancy.first = "empty"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "manned"
      else if baseOccupancy.first is "manned" and baseOccupancy.second is "manned" and baseOccupancy.third is "empty"
        score += 2
        baseOccupancy.first = "empty"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "manned"
      else if baseOccupancy.first is "manned" and baseOccupancy.second is "empty" and baseOccupancy.third is "manned"
        score += 2
        baseOccupancy.first = "empty"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "manned"
      else if baseOccupancy.first is "empty" and baseOccupancy.second is "empty" and baseOccupancy.third is "manned"
        score += 1
        baseOccupancy.first = "empty"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "manned"
      else if baseOccupancy.first is "empty" and baseOccupancy.second is "manned" and baseOccupancy.third is "manned"
        score += 2
        baseOccupancy.first = "empty"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "manned"
      else if baseOccupancy.first is "manned" and baseOccupancy.second is "empty" and baseOccupancy.third is "empty"
        score += 1
        baseOccupancy.first = "empty"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "manned"
      else if baseOccupancy.first is "empty" and baseOccupancy.second is "empty" and baseOccupancy.third is "empty"
        baseOccupancy.first = "empty"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "manned"
    if result is "home run"
      if baseOccupancy.first is "manned" and baseOccupancy.second is "manned" and baseOccupancy.third is "manned"
        score += 4
        baseOccupancy.first = "empty"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "empty"
      else if baseOccupancy.first is "manned" and baseOccupancy.second is "manned" and baseOccupancy.third is "empty"
        score += 3
        baseOccupancy.first = "empty"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "empty"
      else if baseOccupancy.first is "empty" and baseOccupancy.second is "manned" and baseOccupancy.third is "manned"
        score += 3
        baseOccupancy.first = "empty"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "empty"
      else if baseOccupancy.first is "manned" and baseOccupancy.second is "empty" and baseOccupancy.third is "manned"
        score += 3
        baseOccupancy.first = "empty"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "empty"
      else if baseOccupancy.first is "empty" and baseOccupancy.second is "empty" and baseOccupancy.third is "manned"
        score += 2
        baseOccupancy.first = "empty"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "empty"
      else if baseOccupancy.first is "empty" and baseOccupancy.second is "manned" and baseOccupancy.third is "empty"
        score += 2
        baseOccupancy.first = "empty"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "empty"
      else if baseOccupancy.first is "manned" and baseOccupancy.second is "empty" and baseOccupancy.third is "empty"
        score += 2
        baseOccupancy.first = "empty"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "empty"
      else if baseOccupancy.first is "empty" and baseOccupancy.second is "empty" and baseOccupancy.third is "empty"
        score += 1
        baseOccupancy.first = "empty"
        baseOccupancy.second = "empty"
        baseOccupancy.third = "empty"

    return score

  # play inning
  playInning: ->
    outs = 0
    score = 0
    baseOccupancy = { first: "empty", second: "empty", third: "empty" }

    @displayAddGameReport("First Inning")
    while outs < 1
      result = @atbat()
      battingResult = @atBatResult(result, score, baseOccupancy)
      if battingResult.result is "strikeout" or battingResult.result is "pop fly out" or battingResult.result is "ground ball out"
        outs += 1
      @displayAddGameReport("@playInning: outs = #{outs}, score = #{battingResult.score}, bases = #{JSON.stringify(baseOccupancy)}")
      @displayUpdateScore(battingResult.score)
      @displayClearBalls()
      @displayClearStrikes()

  # display
  displayAddBall: (balls) ->
    console.log("#gameplay-at-bat-ball-indicator#{balls}")
    $("#gameplay-at-bat-ball-indicator#{balls}").removeClass("glyphicon glyphicon-unchecked")
    $("#gameplay-at-bat-ball-indicator#{balls}").addClass("glyphicon glyphicon-check")

  displayAddStrike: (strikes) ->
    console.log("#gameplay-at-bat-strike-indicator#{strikes}")
    $("#gameplay-at-bat-strike-indicator#{strikes}").removeClass("glyphicon glyphicon-unchecked")
    $("#gameplay-at-bat-strike-indicator#{strikes}").addClass("glyphicon glyphicon-check")

  displayAddOut: ->
    if $("#gameplay-at-bat-out-indicator1").hasClass("glyphicon-check")
      $("#gameplay-at-bat-out-indicator1").removeClass("glyphicon glyphicon-unchecked")
      $("#gameplay-at-bat-out-indicator1").addClass("glyphicon glyphicon-check")
    else
      $("#gameplay-at-bat-out-indicator2").removeClass("glyphicon glyphicon-unchecked")
      $("#gameplay-at-bat-out-indicator2").addClass("glyphicon glyphicon-check")

  displayClearBalls: ->
    $("#gameplay-at-bat-ball-indicator1").removeClass("glyphicon glyphicon-check")
    $("#gameplay-at-bat-ball-indicator2").removeClass("glyphicon glyphicon-check")
    $("#gameplay-at-bat-ball-indicator3").removeClass("glyphicon glyphicon-check")
    $("#gameplay-at-bat-ball-indicator1").addClass("glyphicon glyphicon-unchecked")
    $("#gameplay-at-bat-ball-indicator2").addClass("glyphicon glyphicon-unchecked")
    $("#gameplay-at-bat-ball-indicator3").addClass("glyphicon glyphicon-unchecked")

  displayClearStrikes: ->
    $("#gameplay-at-bat-strike-indicator1").removeClass("glyphicon glyphicon-check")
    $("#gameplay-at-bat-strike-indicator2").removeClass("glyphicon glyphicon-check")
    $("#gameplay-at-bat-strike-indicator1").addClass("glyphicon glyphicon-unchecked")
    $("#gameplay-at-bat-strike-indicator2").addClass("glyphicon glyphicon-unchecked")

  displayAddGameReport: (report) ->
    $("#gameplay-game-report").append(report + "<br>")

  displayUpdateScore: (score) ->
    $("#gameplay-scoreboard-top-inning-1").text(score)
4

0 回答 0