JavaScript中类似java常量constants使用方法 –.4.12
在写js和后台交互传参的时候,经常要用到一些常量,类似java里面的static属性,如果在js中写死,要是以后常量值改变了,js用这个值得地方又多改起来非常麻烦,老大教了我一种js键值对方式保存常量的方法,觉得非常好,大体思想是创建一个constant.js,在constant.js里定义一个键值类型的对象,然后在需要的页面引用这个js。
有2个用法
*参数值不固定时,往后台传参
*前台翻译
使用方法
var GAMAKER = {begin:"",maker_status_1 :1 //"待审批",maker_status_2 :2 //"已审批",maker_status_3 :3 /*"已驳回"*/,maker_type_1 :"文化",maker_type_2 :"旅游",maker_type_3 :"国际",maker_type_4 :"金融",maker_type_5 :"社会",user_scope_1 :"个人",user_scope_2 :"集体",user_scope_3 :"公司"}}
调用的时候
*往后台传参
$.post("url",{status:GAMAKER['maker_status_1']},function(){},"json");
*前台翻译
var json = [{"id":'1',"name":"a","makerType":1},{"id":'2',"name":"b","makerType":2}{"id":'3',"name":"c","makerType":3}{"id":'4',"name":"d","makerType":4}]/* <select id="select"></select>*/var jsonLength = json.length;while(jsonLength--){var item = json[jsonLength];var option = $("<option></option>");option.html(GAMAKER["maker_type_"item.makerType]);}// 然后就得到了/* <select id="select"><option>文化</option><option>旅游</option><option>国际</option><option>金融</option></select>*/
*主要就这2种使用方式,记下来;