Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Coffee Script allow using Javascript libraries?

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?

like image 551
Clay Nichols Avatar asked Jun 15 '12 16:06

Clay Nichols


People also ask

What are the benefits of CoffeeScript over JavaScript?

The simplicity of the syntax is the beauty of this programming language. The code written in CoffeeScript is very clean and easily understood. No var keyword: Unlike JavaScript, there is no need to use the var keyword before declaring a variable. Thus, it helps to avoid scope declaration issues in a program.

Is CoffeeScript same as JavaScript?

CoffeeScript is a lightweight language that compiles into JavaScript. It provides simple and easy-to-learn syntax avoiding the complex syntax of JavaScript. CoffeeScript is influenced by JavaScript, Ruby, YAML, Haskell, Perl, Python and has influenced MoonScript, LiveScript, and JavaScript.

How do I use CoffeeScript in HTML?

You simple need to add a <script type="text/coffeescript" src="app. coffee"></script> to execute coffee script code in an HTML file. In other cases, I've seen people use the attributes of type="coffeescript" and type="coffee" , so they might work for you as well. Save this answer.


1 Answers

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

like image 193
atmd Avatar answered Oct 26 '22 23:10

atmd