如何改进用 CoffeeScript 编写的 Jquery Widget?

发布于 2024-11-26 16:35:24 字数 3106 浏览 0 评论 0原文

我的目标: 通过将 jquery 小部件移植到 coffescript 来学习 CoffeeScript。

继优秀的 jQuery 小部件介绍之后,我将示例代码移植到了 CoffeeScript: http://bililite.com/blog/understanding-jquery-ui- widgets-a-tutorial/

from:

var Green5  = {
    getLevel: function () { return this.options.level; },
    setLevel: function (x) {
        var greenlevels = this.options.greenlevels;
        var level = Math.floor(Math.min(greenlevels.length-1, Math.max(0,x)));
        this.options.level = level;
        this.element.css({background: greenlevels[level]});
        this._trigger('change', 0, level);
    },
    _init: function() { this.setLevel(this.getLevel()); }, // grab the default value and use it
    darker: function() { this.setLevel(this.getLevel()-1); },
    lighter: function() { this.setLevel(this.getLevel()+1); },
    off: function() {
        this.element.css({background: 'none'});
        this._trigger('done');
        this.destroy(); // use the predefined function
    },
    options: {
        level: 15,
        greenlevels: ['#000','#010','#020','#030','#040','#050','#060','#070','#080','#090','#0a0','#0b0','#0c0','#0d0','#0e0','#0f0', '#fff']
    }
};
$.widget("ui.green5", Green5);

to:

Green5 =
    getLevel: -> @options.level
    setLevel: (x) ->
        greenlevels = @options.greenlevels;
        level = Math.floor(Math.min(greenlevels.length-1, Math.max(0,x)));
        @options.level = level;
        @element.css({background: greenlevels[level]});
        @_trigger('change', 0, level);
    _init: -> @setLevel(@getLevel()) # grab the default value and use it
    darker: -> @setLevel(@getLevel()-1)
    lighter: -> @setLevel(@getLevel()+1)
    off: ->
        @element.css({background: 'none'})
        @_trigger('done')
        @destroy() # use the predefined function
    options: {
        level: 15,
        greenlevels:['#000','#010','#020','#030','#040','#050','#060','#070','#080','#090','#0a0','#0b0','#0c0','#0d0','#0e0','#0f0', '#fff']
    }

$.widget("ui.green5", Green5);

它工作得很好,但是移植起来非常容易,我感觉我还没有得到 CoffeeScript。您将如何改进该代码?

更新(感谢比利!):

Green5 =
    getLevel: -> @options.level
    setLevel: (x) ->
        greenlevels = @options.greenlevels
        level = Math.floor(Math.min(greenlevels.length-1, Math.max(0,x)));
        @options.level = level;
        @element.css
            background: greenlevels[level]
        @_trigger('change', 0, level)
    _init: -> @setLevel @getLevel() # grab the default value and use it
    darker: -> @setLevel @getLevel() - 1 
    lighter: -> @setLevel @getLevel() + 1
    off: ->
        @element.css
            background: 'none'
        @_trigger 'done'
        @destroy() # use the predefined function
    options: {
        level: 15,
        greenlevels:['#000','#010','#020','#030','#040','#050','#060','#070','#080','#090','#0a0','#0b0','#0c0','#0d0','#0e0','#0f0', '#fff']
    }

$.widget "ui.green5", Green5

My Goal:
Learn CoffeeScript by porting a jquery widget to coffescript.

Following the excellent jQuery widget introduction, I have ported the example code to CoffeeScript:
http://bililite.com/blog/understanding-jquery-ui-widgets-a-tutorial/

from:

var Green5  = {
    getLevel: function () { return this.options.level; },
    setLevel: function (x) {
        var greenlevels = this.options.greenlevels;
        var level = Math.floor(Math.min(greenlevels.length-1, Math.max(0,x)));
        this.options.level = level;
        this.element.css({background: greenlevels[level]});
        this._trigger('change', 0, level);
    },
    _init: function() { this.setLevel(this.getLevel()); }, // grab the default value and use it
    darker: function() { this.setLevel(this.getLevel()-1); },
    lighter: function() { this.setLevel(this.getLevel()+1); },
    off: function() {
        this.element.css({background: 'none'});
        this._trigger('done');
        this.destroy(); // use the predefined function
    },
    options: {
        level: 15,
        greenlevels: ['#000','#010','#020','#030','#040','#050','#060','#070','#080','#090','#0a0','#0b0','#0c0','#0d0','#0e0','#0f0', '#fff']
    }
};
$.widget("ui.green5", Green5);

to:

Green5 =
    getLevel: -> @options.level
    setLevel: (x) ->
        greenlevels = @options.greenlevels;
        level = Math.floor(Math.min(greenlevels.length-1, Math.max(0,x)));
        @options.level = level;
        @element.css({background: greenlevels[level]});
        @_trigger('change', 0, level);
    _init: -> @setLevel(@getLevel()) # grab the default value and use it
    darker: -> @setLevel(@getLevel()-1)
    lighter: -> @setLevel(@getLevel()+1)
    off: ->
        @element.css({background: 'none'})
        @_trigger('done')
        @destroy() # use the predefined function
    options: {
        level: 15,
        greenlevels:['#000','#010','#020','#030','#040','#050','#060','#070','#080','#090','#0a0','#0b0','#0c0','#0d0','#0e0','#0f0', '#fff']
    }

$.widget("ui.green5", Green5);

It works perfectly fine, but It was so easy to port that I´ve got the feeling I haven´t gotten CoffeeScript yet. How would you improve on that code?

update (thanks to Billy!):

Green5 =
    getLevel: -> @options.level
    setLevel: (x) ->
        greenlevels = @options.greenlevels
        level = Math.floor(Math.min(greenlevels.length-1, Math.max(0,x)));
        @options.level = level;
        @element.css
            background: greenlevels[level]
        @_trigger('change', 0, level)
    _init: -> @setLevel @getLevel() # grab the default value and use it
    darker: -> @setLevel @getLevel() - 1 
    lighter: -> @setLevel @getLevel() + 1
    off: ->
        @element.css
            background: 'none'
        @_trigger 'done'
        @destroy() # use the predefined function
    options: {
        level: 15,
        greenlevels:['#000','#010','#020','#030','#040','#050','#060','#070','#080','#090','#0a0','#0b0','#0c0','#0d0','#0e0','#0f0', '#fff']
    }

$.widget "ui.green5", Green5

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

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

发布评论

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

评论(1

陪你到最终 2024-12-03 16:35:24

您可以去掉末尾的分号,也可以去掉大量的方括号和花括号。

Green5 =
    getLevel: -> @options.level
    setLevel: (x) ->
        greenlevels = @options.greenlevels;
        level = Math.floor Math.min greenlevels.length-1, Math.max 0,x
        @options.level = level
        @element.css background: greenlevels[level]
        @_trigger 'change', 0, level
    _init: -> @setLevel this.getLevel() # grab the default value and use it
    darker: -> @setLevel this.getLevel() - 1
    lighter: -> @setLevel this.getLevel() + 1
    off: ->
        @element.css background: 'none'
        @_trigger 'done'
        @destroy() # use the predefined function
    options: level: 15, greenlevels: ['#000','#010','#020','#030','#040','#050','#060','#070','#080','#090','#0a0','#0b0','#0c0','#0d0','#0e0','#0f0', '#fff']

$.widget "ui.green5", Green5

然而,语法糖并不是 CoffeeScript 的唯一好处。一个很大的优点是它将某些代码结构重写为更好的 JavaScript。

例如:

yearsOld = max: 10, ida: 9, tim: 11

ages = for child, age of yearsOld
  child + " is " + age

变成:

var age, ages, child, yearsOld;
yearsOld = {
  max: 10,
  ida: 9,
  tim: 11
};
ages = (function() {
  var _results;
  _results = [];
  for (child in yearsOld) {
    age = yearsOld[child];
    _results.push(child + " is " + age);
  }
  return _results;
})();

变量都被正确声明,并且在 ages for 循环 周围自动添加一个闭包,这是用良好的 javascript 技术编写的,老实说,阅读起来相当困难。

You can get rid of semi-colons at the end, and also get rid of a ton of brackets and curly braces.

Green5 =
    getLevel: -> @options.level
    setLevel: (x) ->
        greenlevels = @options.greenlevels;
        level = Math.floor Math.min greenlevels.length-1, Math.max 0,x
        @options.level = level
        @element.css background: greenlevels[level]
        @_trigger 'change', 0, level
    _init: -> @setLevel this.getLevel() # grab the default value and use it
    darker: -> @setLevel this.getLevel() - 1
    lighter: -> @setLevel this.getLevel() + 1
    off: ->
        @element.css background: 'none'
        @_trigger 'done'
        @destroy() # use the predefined function
    options: level: 15, greenlevels: ['#000','#010','#020','#030','#040','#050','#060','#070','#080','#090','#0a0','#0b0','#0c0','#0d0','#0e0','#0f0', '#fff']

$.widget "ui.green5", Green5

However, the syntactic sugar is not the only benefit of CoffeeScript. A big plus is the way it re-writes certain code structures into better javascript.

For example:

yearsOld = max: 10, ida: 9, tim: 11

ages = for child, age of yearsOld
  child + " is " + age

Becomes:

var age, ages, child, yearsOld;
yearsOld = {
  max: 10,
  ida: 9,
  tim: 11
};
ages = (function() {
  var _results;
  _results = [];
  for (child in yearsOld) {
    age = yearsOld[child];
    _results.push(child + " is " + age);
  }
  return _results;
})();

Where the variables are all declared properly, and a closure is automatically added around the ages for loop which is written with good javascript technique that is honestly quite awkward to read.

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