The following CoffeeScript code:
do (a) ->
console.log a
generates this:
(function(a) {
return console.log(a);
})(a);
How do I pass a value to a like this?
(function(a) {
return console.log(a);
})("hello");
CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Specific additional features include list comprehension and destructuring assignment.
CoffeeScript is something that makes even good JavaScript code better. CoffeeScript compiled code can do everything that natively written JavaScript code can, only the code produced by using CoffeeScript is way shorter, and much easier to read.
CoffeeScript. CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque patina, JavaScript has always had a gorgeous heart.
do (a = 'hello') ->
console.log a
Will generate exactly what you want.
Though, i have to admit that i can't see the point of doing that. If you really want a
to take the literal value 'hello'
inside that scope, then why make another scope? With a
being a normal variable declared as a = 'hello'
will be enough. Now, if you want to replace a
with the value of another variable (that might change in a loop or something) and do do (a = b) ->
then i think it makes more sense, but you could simple do do (a) ->
and just use a
instead of b
inside the do
scope.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With