你可以定义一个函数,然后根据参数来判断执行何种操作function fn(type)
{
//公共代码处理,也可以封装方法
    if(type==0)
       alert('1');
    else if(type==1)
        alert('2');
    else
        alert('');
}