Using Underscore.js for CoffeeScript

I am developing in CoffeeScript and want to start using Underscore.js. I know that any JS library will work in CoffeeScript.

There is a regular UnderscoreJS on the net as well as a CoffeScript version. Is there a difference in the implication of the two? Is it okay to use the JS version of underscores for my CoffeeScript needs?

+3


source to share


2 answers


You want to use the JavaScript version. The CoffeeScript version was probably just an author playing with CoffeeScript, which makes sense since he is the author for both CoffeeScript and Underscore. Also, the CoffeeScript version is a compilation step (assuming you are using this in a browser, not a server with node.js).



For another option, check out Lodash. It is a replacement for Underscore and is the best option for many reasons. It just released v1.0 in the last few days.

+8


source


Usually when you are working in Coffeescript you will need something to compile your Coffeescript files along with Javascript in order for the browser to run it. How you want to use the library determines which version you will use.

  • Option 1: Manually add the subtree library (in JS form) as a tag <script>

    in your page, and also add your compiled coffeescript as a tag <script>

    . A quick and easy dirty way to get things working, but leads to tagging <script>

    and <meta>

    additional libraries / styles and spaghetti code added to your page.

  • Option 2: Use a tool to compile all your Coffeescript and CSS into one JS / CSS file, which you then link to in your HTML. Then you will use the Underscore Coffeescript form and compile this with the rest of your code. This is the approach I take, with the added benefit of using features like npm to manage dependencies. Plus, it allows you to have a test web server that compiles your code in real time as you edit Coffeescript. Check out my post on using hem, npm, (and Spine) .



For option 2, you can check something else, requireJS .

0


source







All Articles