在coffeescript中编写jquery插件-如何获取“(函数($)"和“(jQuery)"? [英] Writing a jquery plugin in coffeescript - how to get "(function($)" and "(jQuery)"?

查看:16
本文介绍了在coffeescript中编写jquery插件-如何获取“(函数($)"和“(jQuery)"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 coffeescript 编写一个 jquery 插件,但不确定如何正确获取函数包装部分.

I am writing a jquery plugin in coffeescript but am not sure how to get the function wrapper part right.

我的咖啡脚本是这样开始的:

My coffeescript starts with this:

$.fn.extend({
    myplugin: ->
        @each ->

使用函数包装器创建 javascript:

Which creates the javascript with a function wrapper:

(function() {
  $.fn.extend({
      myplugin: function() {
          return this.each(function() {

但我想要一个像这样传入的$":

but I want a '$' passed in like this:

(function($) {
  $.fn.extend({

我有类似的结局......在coffeescript中没有什么特别的.
我在 javascript 中得到了这个:

Similar for the ending I have... nothing in particular in coffeescript.
I get this in javascript:

})();

但是想要这样:

})(jQuery);

有谁知道如何使用coffeescript 编译器来实现这一点?或者在 coffeescript 中完成这项工作的最佳方法是什么?

Does anyone know how to achieve this with the coffeescript compiler? Or what is the best way to get this done within coffeescript?

推荐答案

答案是你不需要像在 CoffeeScript 中那样调用它——你的脚本已经安全地包装在一个闭包中,所以不需要jQuery-passed-in-as-a-parameter-tricks.随便写:

The answer is that you don't need to call it like that in CoffeeScript -- your script is already safely wrapped in a closure, so there's no need for jQuery-passed-in-as-a-parameter-tricks. Just write:

$ = jQuery

...在脚本的顶部,您可以开始了.

... at the top of your script, and you're good to go.

这篇关于在coffeescript中编写jquery插件-如何获取“(函数($)"和“(jQuery)"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆