Saravanaselvan SP
Saravanaselvan SP

Reputation: 109

Angular Coffeescript to Javascript conversion - next function is considered as the body of the previous function

I am having this strange issue at times when using coffeescript. Example below:

Coffeescript:

$scope.function1 = () -> console.log("function 1")

$scope.function2 = () -> console.log("function 2")

Javascript:

$scope.function1 = function() { console.log("function 1"); return $scope.function2 = function () { console.log("function 1"); }

Why is the second function goes inside the first one? Any help with this is highly appreciated. It is not happening all the time though.

Upvotes: 0

Views: 60

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074038

In CoffeeScript, indentation is meaningful. Your code as posted in your question translates as you want. But if the second function is indented relative to the first:

$scope.function1 = () ->
  console.log("function 1")

  $scope.function2 = () ->
    console.log("function 2")

...it translates incorrectly in the way you've shown.

Be sure also to be consistent in your use of spaces or tabs.

But again, as quoted in the question, it's fine:

$scope.function1 = () ->
    console.log("function 1")

$scope.function2 = () ->
    console.log("function 2")

becomes

$scope.function1 = function() {
  return console.log("function 1");
};

$scope.function2 = function() {
  return console.log("function 2");
};

Upvotes: 3

Related Questions