您的位置:首页 > Web前端 > JavaScript

js 工具类(持续更新)

2015-10-29 10:23 567 查看
//—————————javascript 命名空间————————–

/**
* [GLOBAL 全局变量]
* @type {Object}
*/
var GLOBAL = {};

/**
* [namespace 命名空间生成方法]
* @param  {[type]} str [description]
* @return {[type]}     [description]
*/
GLOBAL.namespace = function (str) {
var arr = str.split("."), o = GLOBAL;
for (var i = (arr[0] == "GLOBAL") ? 1 : 0; i < arr.length; i++) {
o[arr[i]] = o[arr[i]] || {};
o = o[arr[i]];
}
};


//—————————javascript 类继承—————————–

/**
* [extend javascript 类继承方法,兼容低版本IE]
* @param  {[type]} subClass   [子类]
* @param  {[type]} superClass [父类]
* @return {[type]}            [description]
*/
function extend(subClass, superClass) {
var F = function () { };
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
subClass.superClass = superClass.prototype;
if (superClass.prototype.constructor === Object.prototype.constructor) {
superClass.prototype.constructor = superClass;
}
}

function Animal(name) {
this.name = name;
this.type = "animal";
}

Animal.prototype = {
say: function () {
alert(this.type + "," + this.name);
}
};

function Bird(name) {
this.constructor.superClass.constructor.apply(this, arguments);
this.type = "bird";
}

extend(Bird, Animal);

Bird.prototype.fly = function () {
alert("I'm flying");
};

var canary = new Bird("hehe");
canary.say();
canary.fly();

/**
* [extend2 ECMA5 javascript 类继承方法 兼容IE 9+,Firefox 4+,Opera 12+,Chrome]
* @param  {[type]} subType   [description]
* @param  {[type]} superType [description]
* @return {[type]}           [description]
*/
function extend2 (subType,superType) {
var prototype = Object(subType,subType); // 创建对象
prototype.constructor = subType;         // 增强对象
subType.prototype = prototype;           // 指定对象
}

function SuperType(name) {
this.name = name;
this.colors = ["red", "blue", "green"];
}

SuperType.prototype.sayName = function(){
alert(this.name);
};

function SubType(name,age){
SuperType.call(this,name);
this.age = age;
}

extend2(SubType,SuperType);

SubType.prototype.sayAge = function(){
alert(this.age);
};

var instance = new SubType("siri",29);
instance.sayAge();


//—————————datatables 封装工具类————————

/**

* [fnReloadAjax 重新加载datatables插件]

* @param {[type]} oSettings [description]

* @param {[type]} sNewSource [description]

* @param {[type]} fnCallback [description]

* @param {[type]} bStandingRedraw [description]

* @return {[type]} [description]

*/

$.fn.dataTableExt.oApi.fnReloadAjax = function (oSettings, sNewSource, fnCallback, bStandingRedraw) {

if (sNewSource !== undefined && sNewSource !== null) {

oSettings.sAjaxSource = sNewSource;

}
// Server-side processing should just call fnDraw

if (oSettings.oFeatures.bServerSide) {

this.fnDraw();

return;

}
this.oApi._fnProcessingDisplay(oSettings, true);

var that = this;

var iStart = oSettings._iDisplayStart;

var aData = [];

this.oApi._fnServerParams(oSettings, aData);

oSettings.fnServerData.call(oSettings.oInstance, oSettings.sAjaxSource, aData, function (json) {

/* Clear the old information from the table */

that.oApi._fnClearTable(oSettings);

/* Got the data - add it to the table */

var aData = (oSettings.sAjaxDataProp !== “”) ?

that.oApi._fnGetObjectDataFn(oSettings.sAjaxDataProp)(json) : json;

for (var i = 0 ; i < aData.length ; i++) {

that.oApi._fnAddData(oSettings, aData[i]);

}
oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();

that.fnDraw();

if (bStandingRedraw === true) {

oSettings._iDisplayStart = iStart;

that.oApi._fnCalculateEnd(oSettings);

that.fnDraw(false);

}
that.oApi._fnProcessingDisplay(oSettings, false);

/* Callback user function - for event handlers etc */

if (typeof fnCallback == ‘function’ && fnCallback !== null) {

fnCallback(oSettings);

}
}, oSettings);

};

/**

* [myDataTable datatables分页封装]

* @type {Object}
*/

var myDataTable = {

url: “”, // 请求地址

params: null, // 额外参数

columnsSet: null, // 列设置

fnDrawCallback: null, // 重绘的回调函数

init: function () {

(“#editablesample”).dataTable({
“bProcessing”: true,
“bServerSide”: true,
“sAjaxSource”: myDataTable.url, // 后台处理
“aLengthMenu”: [
[5, 15, 20],
[5, 15, 20] // 每页条数
],
“bFilter”: false,
“bSort”: false,
“bStateSave”: true,
“sPaginationType”: “full_numbers”,
“sDom”: ‘rt<”bottom”iflp<”clear”>>’,
“oLanguage”: {
“sUrl”: “../../Admin/js/common/zh-CN.txt”
},
“fnServerData”: function (sSource, aoData, fnCallback, oSettings) { // post 方式 可选
oSettings.jqXHR =.ajax({

“dataType”: ‘json’,

“type”: “POST”,

“url”: sSource,

“data”: aoData,

“success”: function (result) {

fnCallback(result)

}
});

},

“fnServerParams”: myDataTable.params,

“aoColumns”: myDataTable.columnsSet,

“fnDrawCallback”: myDataTable.fnDrawCallback,

});

}
}
//—————————–表单验证工具类———————————-

/**

* [isNumber 判断是否是数字]

* @param {[type]} val [description]

* @return {Boolean} [description]

*/

function isNumber(val) {

var regex = /^[\d|.]+$/;

return regex.test(val);

}
/**

* [isInt 判断是否为整数]

* @param {[type]} val [description]

* @return {Boolean} [description]

*/

function isInt(val) {

var regex = /^\d+$/;

return regex.test(val);

}
/**

* [isEmail 判断是否为邮箱]

* @param {[type]} val [description]

* @return {Boolean} [description]

*/

function isEmail(val) {

var regex = /^\w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+)*$/;

return regex.test(email);

}
/**

* [isMobile 判断是否为手机号]

* @param {[type]} val [description]

* @return {Boolean} [description]

*/

function isMobile(val) {

var regex = /^[1][0-9][0-9]{9}$/;

return regex.test(tel);

}
//———————-字符串操作类——————————-

/**

* [trim trim操作]

* @param {[type]} val [description]

* @return {[type]} [description]

*/

function trim(val) {

return this.replace(/^\s\s*/, ”).replace(/\s\s*$/, ”);

}
/**

* [isEmpty 判断字符串是否为空]

* @param {[type]} val [description]

* @return {Boolean} [description]

*/

function isEmpty(val) {

if (val == null || trim(val).length == 0) {

return true;

}
return false;

}
/**

* [isEquals 判断两个字符串是否相等]

* @param {[type]} val1 [description]

* @param {[type]} val2 [description]

* @return {Boolean} [description]

*/

function isEquals(val1, val2) {

if (val1 === val2) {

return true;

}
return false;

}
/**

* [isEqualsIgnorecase 忽略大小写判断两个字符串是否相等]

* @param {[type]} val1 [description]

* @param {[type]} val2 [description]

* @return {Boolean} [description]

*/

function isEqualsIgnorecase(val1, val2) {

if (val1.toUpperCase() === val2.toUpperCase()) {

return true;

}
return false;

}
/**

* [showError 显示错误信息]

* @param {[type]} obj [html元素]

* @param {[type]} errmsg [错误信息]

* @return {[type]} [description]

*/

function showError(obj, errmsg) {

alert(errmsg);

try {

obj.focus();

} catch (e) {

}


}
//——————————HTML 操作相关工具类—————————–

“`
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javascript