Kevin Meredith
Kevin Meredith

Reputation: 41909

Calling Function from `do` Block

Why is the following code showing an error for unique?

do (unique = (-> ->), x = unique()) ->
  do (y = x) ->
    console.log "x is y:", x is y

output

ReferenceError: unique is not defined

Upvotes: 0

Views: 28

Answers (1)

edi9999
edi9999

Reputation: 20554

I've simplified your code to this (because it is what engenders the error)

do (unique = (-> ->), x = unique()) ->
    console.log "x is y:", x is y

The compiled version is:

(function(unique, x) {
  return console.log("x is y:", x === y);
})((function() {
  return function() {};
}), unique());

Which can also be written like this:

a=function(unique,x) {
      return console.log("x is y:", x === y);
    }
b=(function() {
      return function() {};
    })
a(b,unique)

As you can see, the last unique is not defined anywhere in the scope.

That's why you get this error.

I suggest you to extract unique:

unique = (-> ->)
do (unique, x = unique()) ->
  do (y = x) ->
    console.log "x is y:", x is y

Upvotes: 1

Related Questions