如何改进用 CoffeeScript 编写的 Jquery Widget?
我的目标: 通过将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以去掉末尾的分号,也可以去掉大量的方括号和花括号。
然而,语法糖并不是 CoffeeScript 的唯一好处。一个很大的优点是它将某些代码结构重写为更好的 JavaScript。
例如:
变成:
变量都被正确声明,并且在
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.
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:
Becomes:
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.