I come to this question:
f = (param) ->
console.info '#{param}'
f(1)
The outcome is #{param}
When I enclose the string with double quotation marks, this just print 1
. I have also tested it in Ruby, its behaviour is the same. But that just contradicts the rule in CoffeeScript.org:
The golden rule of CoffeeScript is: "It's just JavaScript".
Because I think in Javascript, single quotes and double quotes are treated equally. And I do not use Ruby often. Can anyone explain why?
Thanks a lot.
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.
In Python, such sequence of characters is included inside single or double quotes. As far as language syntax is concerned, there is no difference in single or double quoted string. Both representations can be used interchangeably.
Enclosing Quotation Marks That means strings containing single quotes need to use double quotes and strings containing double quotes need to use single quotes. "It's six o'clock."; 'Remember to say "please" and "thank you."'; Alternatively, you can use a backslash \ to escape the quotation marks.
In JavaScript, there are three ways to write a string — they can be written inside single quotes ( ' ' ), double quotes ( " " ), or backticks ( ` ` ). The type of quote used must match on both sides, however it is possible that all three styles can be used throughout the same script.
From the CoffeeScript documentation:
Ruby-style string interpolation is included in CoffeeScript. Double-quoted strings allow for interpolated values, using #{ ... }, and single-quoted strings are literal.
"It's just javascript" means it fundamentally compiles to ordinary JavaScript and doesn't attempt to take a radically different programming paradigm and compile it to JavaScript. CoffeeScript is primarily concerned with avoiding "the bad parts", boilerplate, and unnecessary syntax as opposed to introducing radically different basic constructs such as data types, etc.
JavaScript has no string interpolation. CoffeeScript brings this over from Ruby as a convenience. Disabling it for single quotes just gives you a clean way to get a string without the interpolation interpreted.
Don't take It's just JavaScript to mean It IS JavaScript. It's a flavor/variant/sibling.
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