1

I am under the impression that coffeescript translates its code to pure, normal javascript. However, some parts (such as ID selectors), translates in to jQuery ID selectors. This means that I have to link a jQuery script to run this code.

What is the reasoning behind this? Why translate to #(jQuery) ID selector instead of document.getElementByID(normal javascript)?

0

2 Answers 2

3

You're mistaken. ID selectors aren't a part of CoffeeScript syntax, they're a part of jQuery. When you use $('#whatever') in CoffeeScript, you're writing jQuery, not CoffeeScript. CoffeeScript isn't "compiling to jQuery", it's already jQuery.

Additionally, document.getElementById isn't "normal JavaScript". JavaScript is a language, like CoffeeScript is a language. DOM access is an API made available to JavaScript by a browser. Server-side JavaScript, which is every bit as much "normal JavaScript" as in-browser JavaScript, has no such thing as document.

Sign up to request clarification or add additional context in comments.

9 Comments

So you mean, you can write portions of coffeescript in jQuery? Therefore, coffeescript will directly transfer the jQuery instead of translating to javascript?
No, not at all, nothing like that. I'm saying that when you write $(...) or jQuery(...) in CoffeeScript, you're choosing to use jQuery. CoffeeScript doesn't provide any special syntax for selectors, nor could it. CoffeeScript can't know that you intend $ to be used for DOM-querying. I think you're confused bout what jQuery is. It's a library. You don't write things "in jQuery", you write things in either JavaScript or CoffeeScript, and within either of those languages, you use jQuery.
I see. So, is it because coffeescript or javascript is incapable of implementing ID selectors that a jQuery-dependent javascript is needed?
The point is CoffeeScript isn't translating anything when you type $(...). It's not "translating" that to jQuery any more than it could translate that to document.getElementById. It's just a function invocation in CoffeeScript and a function invocation in JavaScript.
That's really still not right. 100% of the stuff you type into CoffeeScript, CoffeeScript can "work on". Anything CoffeeScript can't "work on" is by definition a syntax error. But it happens to work on strings and functions by translating them into strings and functions. That's all a jQuery selector is, a function accepting a string. In CoffeeScript it might look like $ '#myElement' but it's still just a function called $ accepting a string.
|
0

jQuery is a library written in JavaScript, not a language.

We just use this library with normal JavaScript, and it has nothing to do with Coffeescript.

3 Comments

Exactly my thought. Why would coffeescript translate into a jQuery dependent javascript? Is javascript incapable of implementing ID selectors by itself?
Marco, I'm sorry but your observation is plain wrong. See coffeescript.org/#strings
CoffeeScript does not "translate" into a jQuery-dependent JavaScript. It translates into JavaScript. If you use jQuery in your CoffeeScript, you'll need jQuery in your JavaScript, the same as any other library. jQuery isn't magic, it's just plain old JavaScript, and if you use it, you need to include it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.