javascript 写着玩!

本文介绍了一个实用的JavaScript工具库,该库提供了丰富的自定义方法,包括数组操作、类型判断及随机数生成等功能,有助于提高前端开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

;(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;
	})();
})();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值