将 jQuery 移植到 CoffeeScript?

发布于 2024-11-15 06:40:04 字数 385 浏览 1 评论 0原文

CoffeeScript 似乎是一项很有前途的新技术,我肯定会在下一个项目中尝试一下。

看起来 jQuery 可以很容易地与 CoffeeScript 一起使用,但是将 jQuery 完全移植到 CoffeeScript 是否有意义(以同样的方式 下划线 已移植),还是只是浪费时间?

  • 这样一个港口可以带来什么好处呢?
  • 除了可能的兼容性问题之外,这是否也会导致问题(即在 jQuery 的纯 js 中可能使用的技巧,而在 CoffeeScript 中则不可能?)

CoffeeScript seems like a promising new technology, and I'll definitely give it a try in my next project.

It seems like jQuery can be used easily with CoffeeScript as is - however will it make any sense to port jQuery completely to CoffeeScript (in the same manner underscore was ported), or will it be just a waste of time?

  • What advantages would one enjoy from such a port?
  • Apart from possible compatibility issues - could this also cause problems (i.e. tricks possible used in jQuery's pure js which are not possible in CoffeeScript?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

Underscore 的移植只是为了展示 CoffeeScript 的强大功能和简洁性。另外,带注释的 CoffeeScript 源代码为那些熟悉 CoffeeScript 的人提供了更清晰的文档。但官方版本仍然是用纯JS维护的,而不是从CoffeeScript编译的。

我认为,同样,将 jQuery 移植到 CoffeeScript 才有意义,这样好奇的程序员就可以享受带注释的源代码,从而使库的内部工作更加清晰。 jQuery 受益于足够强大的 JavaScript 程序员的关注,我怀疑将其移植到 CoffeeScript 是否会带来任何改进。

不过,我很乐意看到有人这样做,假设他们编写了具有相同行为的漂亮代码。我认为适配器必须仔细考虑的一件事是如何将命名函数(function foo())移植到未命名函数(foo = function()),因为CoffeeScript 仅支持后者(由于某些情况下跨浏览器的不一致),并且两者具有不同的作用域行为。 JavaScript 的 == 应该不是问题,因为我能发现它在 jQuery 源中使用的唯一情况是 x != null,这可以在 CoffeeScript 中完成与x?

Underscore was only ported as a demonstration of CoffeeScript's power and succinctness. Plus, the annotated CoffeeScript source provides clearer documentation for those conversant in CoffeeScript. But the official version is still maintained in pure JS, not compiled from CoffeeScript.

I think that, similarly, it would make sense to port jQuery to CoffeeScript only so that curious coders could enjoy annotated source code that would make the inner workings of the library clearer. jQuery benefits from the attention of enough strong JavaScript programmers that I doubt any improvements would occur from porting it to CoffeeScript.

I'd love to see someone do it, though, assuming they made great-looking code with equivalent behavior. The one thing I think the adapter would have to carefully consider is how to port named functions (function foo()) to unnamed functions (foo = function()), since CoffeeScript only supports the latter (due to cross-browser inconsistencies in some cases) and the two have different scoping behaviors. JavaScript's == shouldn't be an issue, since the only cases I can find it being used in the jQuery source are x != null, which can be done in CoffeeScript with x?.

原谅我要高飞 2024-11-22 06:40:04

更正一下 Coffeescript 是一个 Javascript 生成器。另外更正一下源代码可以更清晰。绝对没有任何可以用 js 完成而不能在 Coffeescript 中完成的技巧 —— 如果需要的话,你可以使用纯 js —— 而且我不确定我是否听说过有人需要这样做。如果您想使用 Javascript,只需将表达式括在反引号中即可:

`var foo = 42`

我不同意代码“更大”。我断言代码中的大多数额外字节都是您应该插入但没有插入的字节——就像在您自己的代码周围添加命名空间包装器一样。在某些情况下,Coffeescript 有聪明的助手,可以提取常见的使用模式,这样它们就不会一次性编码。你可以通过阅读一下生成的 js 来弄清楚这些是什么。

就优势而言,我想这很大程度上取决于您如何看待代码。我热衷于将大量功能封装在类中,并且讨厌 Javascript 基于原型的对象定义。在声明类和继承方面,Coffeescript 更加 Ruby 或 Python 风格。

我给您的建议是: 1) 获取 Trevor Burnham 的 Coffeescript 书,了解 Coffeescript 在实践中的工作原理; 2)拼凑出一些简单的例子来解决您的用例类型; 3)熟悉语法后看看您是否喜欢它。

注意:Coffeescript Google 网上论坛是一个很棒的资源,那里的人们可以快速回答问题 - 很可能有人遇到过让您困惑的问题之前并会分享一个答案。 Trevor 在他的书中列出了一套完整的资源。 (不,我不会因为在书中为他提供道具而获得报酬:)

Correct that Coffeescript is a Javascript generator. Also correct that source code can be clearer. There are absolutely no tricks that can be done in js that can't be done in Coffeescript -- you can drop into pure js if you need to -- and I'm not sure I've ever heard of anyone needing to do this. If you want to drop into Javascript, simply enclose the expression in backticks:

`var foo = 42`

I would disagree that the code is "bigger". I would assert that most extra bytes in the code are ones you should have been inserting anyhow but didn't -- like adding a namespace wrapper around your own code. In some cases, Coffeescript has clever helpers that factor out common usage patterns so they aren't coded one-off. You can figure out which these are by reading the generated js a bit.

In terms of advantages, I guess it depends a lot on how you think about code. I'm a fan of encapsulating a slab of functionality in a class and hate the Javascript prototype-based object definition. Coffeescript is more Rubyish or Pythonesque in terms of declaring classes and also in terms of inheritance.

My advice to you is: 1) Get Trevor Burnham's Coffeescript book and get a taste of how Coffeescript works in practice; 2) Cobble together a few simple examples that address your kind of use case; 3) See how you like it after you get comfortable with the syntax.

Note: The Coffeescript Google Group is a great resource and people answer questions quickly there -- it's likely someone has encountered whatever's puzzling you before and will share an answer. Trevor lists a complete set of resources in his book. (No, I don't get paid to give him props on the book :)

伪心 2024-11-22 06:40:04

考虑到coffescript只是另一种编写javascript的方式,所以你在javascript中可以做的所有事情都可以在coffescript中完成,反之亦然......

很明显,coffescript的唯一优点是更清晰的源代码,这使得它更容易阅读和编码。缺点是 coffescript 生成的 javascript 很可能会比当前的要大得多。对于一个库来说,这是一个相当大的问题...

因此,对于那些试图使用它的人来说,有一个 coffescript 版本作为文档文档会很有趣 。了解 jQuery 的工作原理,但在主代码中使用 coffescript 不会有任何不同

Take into account that coffescript is just another way to code javascript, so everything you can do in javascript can be done in coffescript and viceversa...

Said that is obvious that the only advantage of coffescript is a clearer source code, what makes it easier to read and to code. The disadvantage is that the javascript that coffescript generates will most provably be much bigger that the current.. what for a library is quite a big issue...

So it would be interesting to have one coffescript version as a documentation document for people trying to understand how jQuery works but it wouldn't make any different to use coffescript in the main code

暮年 2024-11-22 06:40:04

我知道我迟到了两年多,但我将 jQuery 移植到了 CoffeeScript。该代码旨在出于比较目的进行查看(jQuery 的 .js 代码与 .coffee 代码),因为我没有专注于添加注释。

查看 http://github.com/sharikul/jQuery-Coffee

I know I'm over two years late but I ported jQuery to CoffeeScript. The code is intended to be viewed for comparison purposes (jQuery's .js code against .coffee code) since I didn't focus on adding comments.

View http://github.com/sharikul/jQuery-Coffee

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文