简体   繁体   English

我怎么能用CoffeeScript写这个呢?

[英]How can I write this in CoffeeScript?

I'm trying to come to terms with CoffeeScript after checking out LESS css yesterday, I was very impressed with that. 我昨天在查看了LESS css后试图与CoffeeScript达成协议,我对此印象非常深刻。

I'm more of a jQuery wiz than Raw Javascript so I find it a little confusing, but at the same time I think looking into CoffeeScript is good as it will help me to get a better understanding by analysing the output. 我更像是一个jQuery wiz而不是Raw Javascript所以我觉得它有点令人困惑,但同时我觉得调查CoffeeScript很好,因为它会帮助我通过分析输出来更好地理解。

var Raw = (function($) {

    $(function() {
        Raw.initialize();
    });

    return {
        _current: '',
        initialize: function() {
            this.initGlobal();
            if(this.is('index')) {
                this.initIndex();
            }
            else if(this.is('single')) {
                this.initSingle();
            }
        },
        initGlobal: function() {
            atom_twitter();
            atom_loading();
            ratings();
        },
        initIndex: function() {
            atom_scroll();
        },
        initSingle: function() {
            atom_download();
        },
        is: function(page) {
            if(this._current == '') {
                this._current = $('body').attr('id');
            }
            return this._current == page;
        }

    };

})(jQuery);

Any ideas where to start? 任何想法从哪里开始?

So far I have this: 到目前为止我有这个:

Raw = (($) ->
  console.log 'hello world'
)(jQuery);

Which outputs: 哪个输出:

(function() {
  var raw;

  raw = (function($) {
    return console.log('hello world');
  })(jQuery);

}).call(this);

Here's how I'd do it: 这是我如何做到的:

raw = do ($ = jQuery) ->
  $ raw.initialize
  {
    _current: ''
    initialize: ->
      @initGlobal()
      if @is 'index'
        @initIndex() 
      else if @is 'single'
        @initSingle()
    # and the rest...
  }

Note that there may be a problem with the code you're trying to port: A function passed to $ will be run when the DOM is ready, or immediately if the DOM already is ready. 请注意,您尝试移植的代码可能存在问题:传递给$的函数将在DOM准备就绪时运行,或者在DOM已经准备就绪时立即运行。 So if the DOM is ready when this code runs, raw.initialize will be called before raw is defined, causing an error. 因此,如果在此代码运行时DOM已准备好,则在定义raw之前将调用raw.initialize ,从而导致错误。

With the way you've structured your code, it is pretty much a line by line conversion. 通过您构建代码的方式,它几乎是逐行转换。 I didn't bother with the $ conversion, but if you want you can just put $ = jQuery just after the do line. 我没有打扰$转换,但如果你想要你可以在do行之后放置$ = jQuery

Raw = null
do ($ = jQuery) ->

  Raw = 
    _current: ''
    initialize: ->
      @initGlobal()
      if @is 'index'
        @initIndex() 
      else if @is 'single'
        @initSingle()

    initGlobal: ->
      atom_twitter()
      atom_loading()
      ratings()
    initIndex: ->
      atom_scroll()
    initSingle: ->
      atom_download()

    is: (page) ->
      @_current = $('body').attr('id') if @_current == ''
      @_current == page

  $ -> Raw.initialize()

you can also use 你也可以用

Raw = (($) ->
  $ ->
    Raw.initialize()

  _current: ""
  initialize: ->
    @initGlobal()
    if @is("index")
      @initIndex()
    else @initSingle()  if @is("single")

  initGlobal: ->
    atom_twitter()
    atom_loading()
    ratings()

  initIndex: ->
    atom_scroll()

  initSingle: ->
    atom_download()

  is: (page) ->
    @_current = $("body").attr("id")  if @_current is ""
    @_current is page
)(jQuery)

if you want to convert your js into coffeescript, use the js2cofee converter on http://js2coffee.org/ 如果你想将你的js转换成coffeescript,请使用http://js2coffee.org/上的js2cofee转换器

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

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