Coffee Script borrowed quite a lot syntax patterns from both Ruby and Python, especially from Ruby.
So people, like me, usually tends to write coffee-script in ruby way.
In ruby, we can retrieve the element in an array in reversed order by using a negative index, which means
array[-1] returns the last element in the array. This grammar sugar is really convenient and powerful, so we can omit the code like this
array[array.length - 1].
But for some reason, coffee-script doesn’t inherit this syntax. To me, it is weird. But after analyze this problem in detail, I found the reason.
Obviously, the previous code should be compiled as following:
The negative index should be processed specially, so we should check the index is negative or not while compiling. This translation seems easy but actually not, since we can and usually use variable as the index.
In the previous code, because we use the variable as index, which cannot be verified in compile-time, which means we need to compile the array reference code as following:
So every time we reference the array, we need to check whether the index is negative or not. This approach absolutely hurts the performance a lot, which in basically unacceptable.
So that’s why coffee-script doesn’t support the negative index.