Viktor
Viktor

Reputation: 4346

Misunderstanding of scopes in CoffeeScript

Why am I getting an error Uncaught TypeError: preload is not a function in the following code?

preload = (event, ui) ->
  preload = new createjs.LoadQueue()
  preload.loadFile('/images/medium/missing.png')
  return

$('.tool').draggable(
  {
    revert: true
    drag: (event, ui) ->
      preload(event, ui)
  })

I thoungt I alredy defined preload(). The function executes anyway despite the error. Where is the catch?

Upvotes: 1

Views: 91

Answers (1)

Satyajeet
Satyajeet

Reputation: 2044

To answer your original question

In most languages with closures (including JavaScript) the inner preload and the outer preload could and would be separate. CoffeeScript though does not allow Shadowing or defining a same variable in a deeper scope And doing it will overwrite the global one. Read This article for more info on coffeescript scoping.

Upvotes: 3

Related Questions