Reputation: 12139
It looks like Google's Dart language doesn't allow you to call native js functions (say, using jQuery or my existing .js code).
Does CoffeeScript allow any of the above?
Upvotes: 5
Views: 3842
Reputation: 7490
yes you can use any javascript library with coffescript, just include the lib the usual way and write your code in the coffeescript 'style', so for a jquery example:
$(function () {
$('.something').on('click', function () {
console.log('you clicked me');
});
});
becomes
$ ->
$(".button").on "click", ->
console.log('you clicked me');
A quick google found some ineresting blog's on the subject, coffeescript & jquery fun and using jquery with coffeescript.
There is also a pragmantic programmer book with a chapter focused on using jquery and backbone in coffeescript applications
n.b. as pointed out, remember that the coffeescript 'compiler' wont check that functions exist, only that the syntax is correct
Upvotes: 10
Reputation: 622
You are also able to use javascript within your coffeescript. All you have to do is use back-ticks:
hello = `function() {console.log("hello")}`
However, there is almost never a good reason to do this. Although there is one scenario where this is useful and that is reusing the same variable name in nested functions.
For example:
parent = ->
outer = 2
changeOuter = ->
`var outer` ##scopes outer to changeOuter
outer = 1
changeOuter()
return outer ##returns 2 but would have returned 1
##if we did not re-scope the varibale
Upvotes: 0
Reputation: 39219
CoffeeScript is JavaScript.
Or rather, more precisely, CoffeeScript's only purpose is to make writing JavaScript an easier, 'cleaner' experience. All the CoffeeScript code you write is compiled into Javascript.
The CoffeeScript compiler only checks your code's syntax. It never bothers to check and see if the variables and functions you're referencing actually exist (this would be impossible to do with running the file anyway). So you can certainly call 'native' JavaScript functions with your CoffeeScript code but that's simple because they come out the other side as simple JavaScript function calls.
Upvotes: 2
Reputation: 96497
You can use jQuery and native JavaScript functions. You simply need to write them in the correct CoffeeScript syntax.
Bear in mind that CoffeeScript is a source to source compiler. It will transpile CoffeeScript to JavaScript. It won't know whether any specified functions exist.
So if you wrote this CoffeeScript, it would compile just fine:
words = ["hello", "world"]
alert word.touppercase() for word in words
Note that touppercase()
is undefined in JavaScript. It should be toUpperCase()
. Nonetheless, CoffeeScript will output:
var word, words, _i, _len;
words = ["hello", "world"];
for (_i = 0, _len = words.length; _i < _len; _i++) {
word = words[_i];
alert(word.touppercase());
}
You would then run into the error as a JavaScript error once you ran this in your browser, not a CoffeeScript error. You can use the "Try CoffeeScript" link on CoffeeScript site to see how the translation takes place and try to run it. You can also try it in jsFiddle by changing the Panels option to use CoffeeScript instead of JavaScript.
Upvotes: 5