文章分类 Classification
javascript常见构建函数
稿件来源: 阳光企业网站管理系统 撰稿作者: 太阳光 发表日期: 2013-11-29 阅读次数: 65 查看权限: 游客查看
javascript常见构建函数
function isString(val){ return val && Object.prototype.toString.call(val) === "[object String]"; } function isNumber(val){ return val && Object.prototype.toString.call(val) === "[object Number]"; } function isArray(val) { return val && Object.prototype.toString.call(val) === '[object Array]'; } function isFunction(val) { return val && Object.prototype.toString.call(val) === '[object Function]'; } function isBoolean(val){ return val === !0 || val === !1 || Object.prototype.toString.call(val) === "[object Boolean]"; } function isDate(val){ return val && Object.prototype.toString.call(val) === "[object Date]"; } function trim(str) { return str.replace(/(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g, ''); } function escape(val) { return val.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"'); } function unescape(val) { return val.replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/&/g, '&'); } function setAttribute(node, name, value) { value == null ? node.removeAttribute(name) : node.setAttribute(name, value) } function getAttrList(tag) { var list = {},reg = /\s+(?:([\w\-:]+)|(?:([\w\-:]+)=([^\s"'<>]+))|(?:([\w\-:"]+)="([^"]*)")|(?:([\w\-:"]+)='([^']*)'))(?=(?:\s|\/|>)+)/g,match; while ((match = reg.exec(tag))) { list[(match[1] || match[2] || match[4] || match[6]).toLowerCase()] = (match[2] ? match[3] : (match[4] ? match[5] : match[7])) || ''; } return list; } function each(obj, fn) { if (obj && Object.prototype.toString.call(obj) === "[object Array]") { for (var i = 0, len = obj.length; i < len; i++) { if (fn.call(obj[i], i, obj[i]) === false) { break; } } } else { for (var key in obj) { if (obj.hasOwnProperty(key)) { if (fn.call(obj[key], key, obj[key]) === false) { break; } } } } } function map(elements, callback){ var value, values = [], i, key; if (typeof elements.length == 'number'){ for (i = 0; i < elements.length; i++) { value = callback(elements[i], i); value != null && values.push(value); } }else{ for (key in elements) { value = callback(elements[key], key); value != null && values.push(value); } } return values; }
function cookies(name,value){ if(value==undefined){ var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)")); if(arr != null) return unescape(arr[2]); return ''; }else{ var exp = new Date(); exp.setTime(exp.getTime() + 24*60*60*1000); document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); } }
Array.prototype.shuffle = function() { var l = this.length; for(var r, i = l ;i--;){ r = parseInt(Math.random() * l);this[r] = [this[i],this[i]=this[r]][0]; } return this; };//打乱数组
function format_time(f,s){ var t = s && new Date(s),k; (!t || t == "Invalid Date") && (t = new Date()); var obj = { Y:t.getFullYear(), M:t.getMonth()+1, D:t.getDate(), h:t.getHours(), m:t.getMinutes(), s:t.getSeconds() }; return f.replace(/([a-zA-Z])/g,function(a){ k = obj[a];return k ? (a == "Y" ? k : ("0" + k).slice(-2)) : a; }); }//格式化时间
var cookie={ set:function (n, j, m) { var t = new Date; document.cookie = n + "=" + encodeURIComponent(j || "") + ( m ? "; expires=" + t.setTime(t.getTime() + 1000*60*m).toGMTString() : ""); //domain=" + (m || location.host) + (l ? "; secure":"") }, get:function (a) { return(a = document.cookie.match(RegExp("(^|;\\s)" + a + "=([^;]*)", "i"))) ? decodeURIComponent(a[2]) : ""; } }; //构建Array.some if (!Array.prototype.some){ Array.prototype.some = function(fun){ if ("function" != typeof fun)return; var len = this.length,thisp = arguments[1]; for (var i = 0; i < len; i++){ if (i in this && fun.call(thisp, this[i], i, this)) return true; } return false; }; } if (!Array.prototype.every){ Array.prototype.every = function(fun){ 'use strict'; if (this === void 0 || this === null) throw new TypeError(); var t = Object(this); var len = t.length >>> 0; if (typeof fun !== 'function') throw new TypeError(); var thisArg = arguments.length >= 2 ? arguments[1] : void 0; for (var i = 0; i < len; i++){ if (i in t && !fun.call(thisArg, t[i], i, t)) return false; } return true; }; }
Function.prototype.method = function (name, fn) { typeof this.prototype[name] == "undefined" && (this.prototype[name] = fn); return this }; Number.method("add",function(n){ return this + (n ? n : 0); }); console.log((5).add(2));
关键词: js,函数,构建函数 编辑时间: 2014-04-03 15:03:42
0
高兴0
支持0
搞笑0
不解0
谎言0
枪稿0
震惊0
无奈0
无聊0
反对0
愤怒
0%(0)
0%(0)
- 中搜索:javascript常见构建函数
- 中搜索:javascript常见构建函数
- 暂无评论
文章图片 article Pictrue
网友评论