Consider the following CoffeeScript:
$ ->
if localStorage["Flag1"] isnt "Done"
localStorage["Flag1"] = "Done" # Flagged on first page loading
$(".start").click ->
if localStorage["Flag2"] isnt "Done"
localStorage["Flag2"] = "Done" # Flagged on first click interaction
Which compiles into:
$(function() {
if (localStorage["Flag1"] !== "Done") {
localStorage["Flag1"] = "Done";
}
return $(".start").click(function() {
if (localStorage["Flag2"] !== "Done") {
return localStorage["Flag2"] = "Done";
}
});
});
There are two strange occurrence of “return” being planted into the rendered JavaScript. What do they do, and how will they affect the running of the script? Thanks!
They won’t affect the running of your script. The first
returnwill return$(".start")(since the jQueryclickmethod returns an instance of jQuery) from the DOM ready event handler. Since it’s a callback that runs at a certain point, you can’t really do anything with that return value.The second
returnwill return"Done", after setting thelocalStorageproperty, but again, since it’s returning from a callback (a click event handler this time) you won’t be able to do anything with the returned value.I believe CoffeeScript will
returnthe value of the last expression in each function, which is why you see thosereturnstatements in the output. From the docs: