我找到了这个CoffeeScript插件的jQuery样板,我一直在学习,并试图在我试图编写的插件中使用它。我在其他几个问题中引用了同样的样板/模板。我是JavaScript的业余爱好者,也是CoffeeScript的新手.我试着学习和学习,但当一些事情困扰我,我无法通过谷歌找到满意的答案,我来到这里.因此,请原谅我在这里编写的代码中缺乏知识和潜在的错误。
CoffeeScript代码编译如下:
(function() {
(function($, window, document) {
var $this, methods, _anotherState, _flag, _internals, _settings;
$this = void 0;
_settings = {
"default": 'cool!'
};
_flag = false;
_anotherState = null;
methods = {
init: function(options) {
$this = $(this);
$.extend(_settings, options || {});
return $this;
},
doSomething: function(what) {
return $this;
},
destroy: function() {
return $this;
}
};
_internals = {
toggleFlag: function() {
return _flag = !_flag;
},
computeSomething: function(state, flag) {
return flag != null ? flag : {
state: "No, that's not right."
};
}
};
return $.fn.foobar = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === "object" || !method) {
return methods.init.apply(this, arguments);
} else {
return $.error("Method " + method + " does not exist on jquery.foobar");
}
};
})(jQuery, window, document);
}).call(this);从这里和这里了解到,(function(){...}).call(this)包装器是一个CoffeeScript特性,用于本地化未显式声明为全局的变量。后来我了解到,在编译过程中,它也可以被抑制。我还了解到,我不需要将window和document作为jQuery闭包的参数。
当我更多地研究它(并试图编辑它)时,我看到在编译的代码中,闭包(它是一个函数)在定义它的地方返回$.fn.foobar。因为这个函数是匿名的,而且无论如何也不会被调用,所以我想返回的值并不重要。但是,如果我将一个return语句放在CoffeeScript代码中,那么会怎么样呢:
$.fn.foobar = (method) ->
if methods[method]
methods[method].apply this, Array::slice.call(arguments, 1)
else if typeof method is "object" or not method
methods.init.apply this, arguments
else
$.error "Method " + method + " does not exist on jquery.foobar"
# -- This, right here...
return
# --它不再编译为return $.fn.foobar = ...,而是只编译$.fn.foobar = ...。我认为这没有什么区别,而是让JS输出更多.干净..。如果你愿意。但我得确认一下。这对脚本的执行有什么影响?
此外,在CoffeeScript代码中,作者说,在methods.init()中,我需要在$this.each上执行所有的操作,但是如果我这样做了
init: (options) ->
$.extend _settings, (options or {})
return $(@).each () -> # I don't really need return, do I?
# In here @ is one element (out of the array of elements)
return # This is to suppress any returns inside .each()所以这就是..。这是我的问题
return代码中的匿名函数进行CoffeeScript?这与原始的CoffeeScript代码到底有什么不同?注意到:为了避免文章太长,我没有包含CoffeeScript代码。但是我提供了一个链接到代码列出的页面。但是,如果这是一个麻烦,请在评论中告诉我,我也会编辑这个帖子以包含CoffeeScipt代码。谢谢您抽时间见我。
发布于 2014-05-29 05:44:55
下面是我是如何做到这一点的(请参阅GitHub中的整个源代码包括如何使用QUnit对Coffeescript jQuery插件进行单元测试的示例,以及如何将其构建成使用sourceMap的小型化版本)。
throw "Expected jQuery to have been loaded before this script." if typeof jQuery isnt "function"
(($) ->
# Main jQuery Collection method.
$.fn.myPlugin = (options) ->
self = this
opts = $.extend true, {}, $.fn.myPlugin.options
@options = if typeof options is "object"
$.extend(true, opts, options)
else
opts
@each ->
$this = $(this)
# TODO: do pluginy stuff with $this
return $this # because it's chainable.
# defaults
$.fn.myPlugin.options =
whatever: true
) jQuery相应的QUnit测试看起来像
(($) ->
module "basic tests",
setup: ->
this.elems = $("#qunit-fixture").children(".qunit-container")
# all jQuery plugins must be chainable.
test "is chainable", ->
expect(1)
strictEqual(this.elems.myPlugin(), this.elems, "should be chainable")
) jQuery我希望这能帮上忙。
https://stackoverflow.com/questions/18190587
复制相似问题