0

This code gives me error:

TypeError: oneTime is not a function oneTime();

$ ->
  value = $("#car_addition_price_type").val()
  if value == "one_time"
    oneTime()
  else if value == "by_day"
    byDay()
  else
    hideAll()

  hideAll = ->
    $('#by_day').hide()
    $('#by_day input').val('')
    $('#max_price').hide()
    $('#max_price').val('')

  byDay = ->
    $("label[for='car_addition_price']").text("Cena/dzień")
    $('#max_price').show()
    $('#by_day').show()  

  oneTime = ->
    $('#by_day').show()
    $("label[for='car_addition_price']").text("Cena jednorazowa")
    $('#max_price').hide()
    $('#max_price input').val('')  

Question is simple. Why?

4
  • Yep, stupid error. Thank's :) Commented Apr 23, 2014 at 12:50
  • 1
    Are these functions functions declaration or expression? For these functions to get hoisted, they have to be function declaration. So in your case declare your functions on top. Commented Apr 23, 2014 at 13:12
  • @AlexShilman oneTime = -> is converted to a function expression, the var oneTime gets hoisted (of course) but the assignment stays where it is. Commented Apr 23, 2014 at 17:04
  • So that's the problem. ;) Commented Apr 23, 2014 at 17:25

1 Answer 1

1

So just to post this answer if it's not clear yet. The problem here is that your functions are functions expressions. So the declaration gets hoisted to the top but the assignment happens after your call. So the function doesnt exist at the time of your call. So in your case either make it a function declaration like so:

function oneTime(){}

or if you want to keep your functions as is, then just move all your functions to the top, above this block of code:

if value == "one_time"
oneTime()
 else if value == "by_day"
  byDay()
else
  hideAll()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.