我绝不是程序员...我从来没有真正参加过comp sci课程,也不太了解理论,但我仍然将通常可行的代码整合在一起。我只是不知道它到底有多难看。我最近写了这个(简单)的JS,并且想知道我可以得到一些反馈......
如果这不适合它,请告诉我。感谢。
- 威尔
/*
@author: Will Cavanagh
@date: 2009-09-14
* Custom scroll box
*/
var customScroller = {
intRegex : /^\d+$/,
maxScroll : 0,
inited : false,
upColor : "#FFF",
downColor : "#FFF",
defSpeed : "#FFF",
//init function -- sets config values and initallizes jQuery slider.
//@param options : object containing set up parameters
//@return null
init : function(options) {
//if there are no options/colors specified, make empty object
if(!options) { options = new Object(); };
if(!options.scrollerColor) { options.scrollerColor = new Object(); };
//assign variables, use defaults if not defined.
var width = this.intRegex.test(options.width) ? options.width : 500
var height = this.intRegex.test(options.height) ? options.height : 300;
var vertical = options.vertical == null ? true : options.vertical;
upColor = options.scrollerColor.upColor == null ? '#4a4a4a' : options.scrollerColor.upColor;
downColor = options.scrollerColor.downColor == null ? '#333' : options.scrollerColor.downColor;
var bkgdColor = options.scrollerColor.bkgdColor == null ? '#848484' : options.scrollerColor.bkgdColor;
defSpeed = options.defaultSpeed == null ? '1000' : options.defaultSpeed;
//set content width before measuring
jQuery("#content-scroll").css({width: width});
//get height of content, subtract height of pane to be shown in
maxScroll = jQuery("#content-scroll").height() - height;
//set the height of pane to hold content. This is done after measuring content height for browser compatability reasons
jQuery("#content-scroll").css({width: width, height: height});
if(this.vertical) {
var orientation = 'vertical';
} else {
var orientation = 'horizontal';
}
//create the JQuery.UI slider
jQuery("#content-slider").slider({
value: 100,
orientation: 'vertical',
animate: false,
change: customScroller.handleSliderChange,
slide: customScroller.handleSliderSlide,
start: customScroller.handleSliderStart,
stop: customScroller.handleSliderStop
});
jQuery(".ui-slider-handle").css({background:upColor});
jQuery("#slider-bkg").css({background:bkgdColor});
$("#content-scroll").mousewheel(function(objEvent, intDelta){
scroll(intDelta * -30, 0);
});
inited = true;
},
//animates a scroll to the beginning of the content
//@return null
goTop : function() {
if(!inited) { return };
var scrollto = 0;
jQuery("#content-scroll").animate({scrollTop: scrollto}, {queue:false, duration:defSpeed});
jQuery("#content-slider").slider('option', 'value', 100*(1-(scrollto/maxScroll)));
},
//handler function bound to a slider change event.
//@param e : event
//@param ui : slider ui object
//@return null
handleSliderChange : function(e, ui) {
if(!inited) { return };
jQuery("#content-scroll").animate({scrollTop: ((100-ui.value) / 100) * (maxScroll) }, {queue:false, duration:defSpeed});
},
//handler function bound to a slider slide event.
//@param e : event
//@param ui : slider ui object
//@return null
handleSliderSlide : function(e, ui) {
if(!inited) { return };
jQuery("#content-scroll").attr({scrollTop: ((100-ui.value) / 100) * (maxScroll) });
},
//handler function bound to a slider start of slide event.
//@return null
handleSliderStart : function() {
if(!inited) { return };
jQuery(".ui-slider-handle").css({background:downColor});
},
//handler function bound to a slider end of slide event.
//@return null
handleSliderStop : function() {
if(!inited) { return };
jQuery(".ui-slider-handle").css({background:upColor});
},
//scroll by a given amount.
//@param amt : amount to scroll by
//@param speed : sroll animation speed, defaults to default speed defined in init params
//@return null
scroll : function(amt, speed) {
if(!inited) { return };
if(!speed) { speed = defSpeed; }
var scrollto = jQuery("#content-scroll").scrollTop() + amt;
if(scrollto > (maxScroll - 20)) scrollto = maxScroll; //near or past end of content, scroll to end
if(scrollto < 20) scrollto = 0; //near or past beginning of content, scroll to beginning
jQuery("#content-scroll").animate({scrollTop: scrollto}, {queue:false, duration:speed}); //animate content scroll
jQuery("#content-slider").slider('option', 'value', 100*(1-(scrollto/maxScroll))); //update slider position
}
}
答案 0 :(得分:5)
几点提示:
new Object();
可以安全地替换为对象文字 - { }
。upColor
和downColor
。使用var
声明它们。20
之类的“神奇数字”(scrollto < 20
)。在描述性名称下单独定义它们。null
)似乎没有必要与options.scrollerColor.upColor == null
进行比较。我会删除null
并利用隐式类型转换(这也会捕获空字符串!)答案 1 :(得分:2)
除了kangax所说的,这对于定义默认值有点笨拙:
var width = this.intRegex.test(options.width) ? options.width : 500;
var height = this.intRegex.test(options.height) ? options.height : 300;
var vertical = options.vertical == null ? true : options.vertical;
更好的方法是使用||操作者:
var width = options.width || 500;
var height = options.height || 300;
var vertical = options.vertical || true;
更好的方法是使用默认选项获取对象并将这些选项与实际提供的选项相结合的函数:
var defaultOptions = {width: 500, height: 300, vertical: true};
var options = applyDefaults(defaultOptions, options);
也...
您可能需要考虑完全删除这些if(!inited) { return }
行,或者将其替换为if(!inited) { alert("not inited"); }
,否则您将屏蔽错误。当有人试图使用你的customScroller
但忘记运行init()
时,那么目前这个东西不起作用,甚至不会出错,而且很难找到为什么一切都在默默地失败。最大的失败是巨大的噪音。
还有更多......
您似乎确实需要考虑采用更加面向对象的方法。目前,您拥有所有这些全局变量inited
,upColor
,downColor
等,它们确实非常想成为实例变量。说,像这样:
var CustomScroller = function(options) {
this.options = options;
};
CustomScroller.prototype = {
init: function() {
...
this.inited = true;
},
goTop: function() {
if (!this.inited) { alert("Not inited!"); }
...
}
};
var myScroller = new CustomScroller({width: 100, height: 300, ...});
myScroller.init();
myScroller.goTop();
答案 2 :(得分:0)
主要是,不要害怕空白。将块的语句放在与表达式不同的行上,可以更容易地查看表达式的结束位置和语句的开始。
示例:
if(expression) {
statement;
}
vs
if(expression){ statement; }
在块语句中始终使用括号也是一种好习惯,这可以防止大量错误。
例如
if(expression) {
statement;
statement;
}
vs
if(expression)
statement;
statement; //woops
你有很多if-not -return结构,if-do会更少线条和更清晰。
例如
if(!expression) {
return;
}
statement;
vs
if(expression) {
statement;
}