;(function() {
return window.Query = window.$ = (function() {
try {
var _this = {};
/**
* 循环数组或者名值对象
*/
_this.each = function(obj, callback, args) {
if (_this.isObj(obj) && !args) {
for (var idx in obj) {
if (callback.call(obj[idx], idx, obj[idx]) === false) {
break;
}
}
} else {
var idx = 0;
for (var o = obj[idx]; idx < obj.length && (callback.call(o, idx, o) !== false); o = obj[++idx]) {
}
}
return _this;
}
/**
*判断对象是数组还是名值对象
* @param {Object} obj
*/
_this.isObj = function(obj) {
return typeof obj == "object" && !(_this.isArray(obj)) ? true : false;
}
/**
*为本地对象添加合适的方法
*/
_this._addFunForLocalObject_ = function() {
/**
*为数组添加indexof方法
*/
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(v) {
for (var idx in this) {
if (this[idx] === v)
return idx;
}
return -1;
}
}
/**
*为数组添加remove方法
*/
if (!Array.prototype.remove) {
Array.prototype.remove = function(v) {
var vidx = this.indexOf(v);
if (vidx >= 0)
this.splice(vidx, 1);
}
}
/**
*为数组添加contain方法
*/
if (!Array.prototype.contain) {
Array.prototype.contain = function(v) {
var vidx = this.indexOf(v);
if (vidx >= 0)
return true;
}
}
/**
*为所有的对象添加显示当前值的方法
*/
if (!Object.prototype.val) {
Object.prototype.val = function() {
return this.valueOf();
}
}
/**
*为String对象添加转换为Int的方法
*/
if (!String.prototype.intVal) {
String.prototype.intVal = function() {
return parseInt(this);
}
}
return _this;
}();
/**
*判断是否为数字
*/
_this.isNumber = function(v) {
return !v || isNaN(v.valueOf()) ? false : ( typeof v.valueOf() === "number");
}
/**
*判断是否为数组
*/
_this.isArray = function(v) {
return !v || !( v instanceof Array) ? false : true;
}
/**
*判断数组中所有的值是否全是数字
*/
_this.isNumberArray = function(v) {
var a = true;
for (var idx = 0; idx < v.length; idx++) {
a = a && _this.isNumber(v[idx]);
}
return !v || !_this.isArray(v) ? false : a;
}
/**
*判断是否为字符串
*/
_this.isString = function(v) {
return !v || ( typeof v.valueOf() !== "string") ? false : true;
}
/**
*判断是否为字符串
*/
_this.isBooblean = function(v) {
return typeof v.valueOf() !== "boolean" ? false : true;
}
/**
*用于处理字符串拼接
*/
_this.StringBuffer = function() {
this._strings_ = new Array;
if (this._inited === undefined) {
this.append = function(v) {
this._strings_.push(v);
}
this.toString = function() {
return this._strings_.join("");
}
this._inited = true;
}
}
/**
*生成从vf到vl之间的随机数,isDecimal表示是否保留小数
*/
_this._protectRandom_ = function(vf, vl, isDecimal) {
var n = Math.random() * (vl - vf + 1) + vf;
return isDecimal ? n : Math.floor(n);
}
/**
*生成大于给定数字的随机数
*/
_this._gtRandom_ = function(v, isDecimal) {
var n = Math.random() * v + v;
return isDecimal ? n : Math.round(n);
}
/**
*生成小于给定数字的随机数
*/
_this._ltRandom_ = function(v, isDecimal) {
var n = Math.random() * v;
return isDecimal ? n : Math.round(n);
}
/**
*数组copy
*/
_this.arrCopy = function(src, target) {
if (src && target) {
_this.each(src, function(idx, o) {
target[idx] = o;
});
return target;
}
}
/**
*将参数转换为数组类型
*/
_this._argsToArray_ = function(v) {
var result = new Array
for (var idx = 0; idx < v.length; idx++) {
result.push(v[idx]);
}
return result;
}
/**
*生成随机数或者选出随机数
*/
_this.random = function() {
var args = arguments;
var length = args.length;
if (length && length <= 1) {
return _this.isArray(args[0]) ? args[0][_this._protectRandom_(0, args[0].length - 1)] : Math.random();
} else {
args = _this._argsToArray_(args);
if (_this.isNumberArray(args)) {
return length === 2 ? _this._protectRandom_(Math.min(args[0], args[1]), Math.max(args[0], args[1]), 1) : args[_this._protectRandom_(0, length - 1)];
} else {
var g = args.indexOf("gt") >= 0;
var l = args.indexOf("lt") >= 0;
var h = _this.isNumber(args[0]);
var i = g && h;
var m = l && h;
if (length === 2) {
return i ? _this._gtRandom_(args[0], 0) : m ? _this._ltRandom_(args[0], 0) : args[_this._protectRandom_(0, length - 1)];
} else if (length === 3) {
var f = _this.isBooblean(args[length - 1]);
var d = _this.isNumberArray(args.slice(0, 2));
return f && d ? _this._protectRandom_(Math.min(args[0], args[1]), Math.max(args[0], args[1]), args[2]) : f && i ? _this._gtRandom_(args[0], args[2]) : f && m ? _this._ltRandom_(args[0], args[2]) : args[_this._protectRandom_(0, length - 1)];
} else {
return args[_this._protectRandom_(0, length - 1)];
}
}
}
}
window.$.prototype = _this;
} catch(e) {
}
return _this;
})();
})();
javascript 写着玩!
最新推荐文章于 2025-11-30 22:19:10 发布
本文介绍了一个实用的JavaScript工具库,该库提供了丰富的自定义方法,包括数组操作、类型判断及随机数生成等功能,有助于提高前端开发效率。
2272

被折叠的 条评论
为什么被折叠?



