动态的声明JS 方法(define functions in javascript dynamically )
访问量: 2171
下面的代码是不是看起来很可恶? (isn't it too smelly? )
restart_services_animation = function(){
high_light_link_on_right_menu('services')
if($('#services_animation_is_showing').length > 0 || $('#services_animation_is_shown').length < 1) {
return;
}
cancel_services_animation()
start_services_animation()
}
restart_cases_animation = function(){
high_light_link_on_right_menu('cases')
if($('#cases_animation_is_showing').length > 0 || $('#cases_animation_is_shown').length < 1) {
return;
}
cancel_cases_animation()
start_cases_animation()
}
动态的声明 它们: ( dynamically define functions as below)
// 这里是动态的方法声明和调用。 声明以下四个方法:
// restart_index_animation()
// restart_services_animation()
// restart_cases_animation()
// restart_about_us_animation()
$(['index', 'services', 'cases', 'about_us']).each(function(i,e){
window['restart_' + e + '_animation'] = function() {
high_light_link_on_right_menu( e )
if($('#' + e + '_animation_is_showing').length > 0 ||
$('#' + e + '_animation_is_shown').length < 1) {
return;
}
// 动态的方法调用,例如: cancel_services_animation()
window['cancel_'+e+'_animation']()
// 动态的方法调用,例如: start_services_animation()
window['start_' + e + '_animation']()
}
})
see: http://stackoverflow.com/questions/8309242/how-to-dynamically-build-a-function-in-javascript