JS 公共方法

1、Format函数

$.GM.Format = function (source, params) {
if (arguments.length == 1)
return function () {
var args = $.makeArray(arguments);
args.unshift(source);
return $.format.apply(this, args);
};
if (arguments.length > 2 && params.constructor != Array) {
params = $.makeArray(arguments).slice(1);
}
if (params.constructor != Array) {
params = [params];
}
$.each(params, function (i, n) {
source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
});
return source;
}

调用:

var strTableTemplate = "<td>{0}-{1}-{2}</td>"
 var strTemplate = $.GM.Format(strTableTemplate, str1,str2,str3);

2、GenerateGuid函数(生成随即GUID)

 $.GM.NewGuid = function () {
var guid = "";
for (var i = 1; i <= 32; i++) {
var n = Math.floor(Math.random() * 16.0).toString(16);
guid += n;
if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
guid += "-";
}
return guid;
};

3、获取Url里面的get提交参数

   $.Gmfp.GetUrlParams = function () {
var strRawUrl = decodeURI(window.location);
var regex = /^[^&]+?[?]([\s\S]*)$/g;
var matches = regex.exec(strRawUrl);
if (matches && matches.length >= ) {
var strParams = matches[];
regex = /([^&=]+)=([^&=]*)/g;
var resObj = {};
while (matches = regex.exec(strParams)) {
resObj[matches[]] = matches[];
}
return resObj;
}
else {
return {};
}
}
上一篇:boost asio 学习(二)了解boost::bind


下一篇:ext之关键字mixins、statics、require