为js数组扩展方法
2016-03-03 15:13
555 查看
(function(global,undefined){ //javascript冒泡排序,直接添加到基础类型Array的原型上 Function.prototype.method = function (name, func) { //if(!this.prototype[name]){ //先判断一下是原型中否有这个方法,如果没有再添加 this.prototype[name] = this.prototype[name] || func; //} return this; }; /*Function.prototype.tool = { method:function (name, func) { if(!this.prototype[name]){ //先判断一下是原型中否有这个方法,如果没有再添加 this.prototype[name] = func; } return this; } }; tool.*/ Object.method('toString',function(){alert(33)}); /*判断Array、Function、Object、String、Number、Null、undefined、boolean类型*/ Object.method('getType',function(){ if(typeof this == "object"){ var type = Object.prototype.toString.call(this); return type.split(" ")[1].replace("]",""); }else{ return typeof this; } }); //冒泡算法,左边一项跟右边每一项比 Array.method('bubble',function(){ var len = this.length; for (j=0 ; j < len; j++) { for (i=j+1; i<len;i++){ var first = this[i], sec = this[j]; if (sec>first){ var tmp = this[i]; this[i] = sec; this[j] = tmp; } }; }; return this; }); //获取数组最大值 Array.method('max',function(){ return Math.max.apply(Math,this); }); //获取数组最小值 Array.method('min',function(){ return Math.min.apply(Math,this);; }); //删除数组中含有指定内容的一项 Array.method('delByCnt',function(cnt,flag){ if(typeof flag == 'boolean'){ if(flag){ this.splice(this.indexOf(cnt),1); return this; }else{ return this.splice(this.indexOf(cnt),1); } }else{ throw new Error("delByCnt方法的第二个参数必须是boolean类型!"); } }); })(this);
相关文章推荐
- 枚举类转成json
- servlet与jsp的生命周期
- [JavaScript][表单文本框&下拉列表框操作]
- js中怎么样取到本div下面的第一个span里面的值
- How to handle javascript alerts, confirmation and prompts?
- js获取节点
- 字符串和JSON对象互转的方法
- Javascript进阶篇——( JavaScript内置对象---下)--Array数组对象---笔记整理
- angular js 在ie11 下的get请求缓存问题的解决办法
- 关于ActiveX控件开发、js调用
- C# JSON和对象之间互相转换
- javascript中eval的用法
- javascript 高级程序设计 五
- [LeetCode][JavaScript]Palindrome Number
- javascript阻止事件冒泡和浏览器的默认行为
- js 时间操作
- [JavaScript][window&forms]
- javascript 封装ajax(多版本)
- com.fasterxml.jackson.core.JsonParseException: Unexpected character
- javaScript中的两个叹号(!!)