简体   繁体   English

CoffeeScript在JS中转换为jQuery

[英]CoffeeScript translating to jQuery in JS

I am under the impression that coffeescript translates its code to pure, normal javascript. 我的印象是coffeescript将其代码转换为纯正常的javascript。 However, some parts (such as ID selectors), translates in to jQuery ID selectors. 但是,某些部分(例如ID选择器)会转换为jQuery ID选择器。 This means that I have to link a jQuery script to run this code. 这意味着我必须链接一个jQuery脚本来运行这段代码。

What is the reasoning behind this? 这背后的原因是什么? Why translate to # (jQuery) ID selector instead of document.getElementByID (normal javascript)? 为什么要转换为# (jQuery)ID选择器而不是document.getElementByID (普通的javascript)?

You're mistaken. 你错了。 ID selectors aren't a part of CoffeeScript syntax, they're a part of jQuery. ID选择器不是CoffeeScript语法的一部分,它们是jQuery的一部分。 When you use $('#whatever') in CoffeeScript, you're writing jQuery, not CoffeeScript. 当你在CoffeeScript中使用$('#whatever')时,你正在编写jQuery,而不是CoffeeScript。 CoffeeScript isn't "compiling to jQuery", it's already jQuery. CoffeeScript不是“编译为jQuery”,它已经是 jQuery了。

Additionally, document.getElementById isn't "normal JavaScript". 此外, document.getElementById不是“普通JavaScript”。 JavaScript is a language, like CoffeeScript is a language. JavaScript是一种语言,就像CoffeeScript是一种语言。 DOM access is an API made available to JavaScript by a browser. DOM访问是浏览器为JavaScript提供的API。 Server-side JavaScript, which is every bit as much "normal JavaScript" as in-browser JavaScript, has no such thing as document . 服务器端JavaScript,与浏览器中的JavaScript一样多的“普通JavaScript”,没有document这样的东西。

jQuery is a library written in JavaScript, not a language. jQuery是一个用JavaScript编写的库,而不是一种语言。

We just use this library with normal JavaScript, and it has nothing to do with Coffeescript. 我们只使用普通JavaScript的这个库,它与Coffeescript无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM