Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array splicing with coffeescript, what is _ref for?

In the CoffeeScript docs for array splicing, what is the purpose of the trailing , _ref?

CoffeeScript:

numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers[3..6] = [-3, -4, -5, -6]

Compiles to:

var numbers, _ref;
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
[].splice.apply(numbers, [3, 4].concat(_ref = [-3, -4, -5, -6])), _ref;
like image 773
AJP Avatar asked Jul 28 '12 11:07

AJP


People also ask

What can you do with CoffeeScript?

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.

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

That's because CoffeeScript's slicing operation wants to return the slice it has just assigned, but splice() returns the removed elements instead.

So, in order to achieve this, it compiles the construct into a code fragment that first assigns the slice to a local _ref variable, then uses the comma operator to return that variable after calling splice().

like image 69
Frédéric Hamidi Avatar answered Sep 28 '22 01:09

Frédéric Hamidi