Skip to content

Instantly share code, notes, and snippets.

@justpsvm

justpsvm/tool.js

Last active Dec 9, 2017
Embed
What would you like to do?
[js 中常用的扩展方法] #js
//添加数组的移除函数
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
//添加 indexOf 函数功能
Array.prototype.indexOf = function(val,name) {
for (var i = 0; i < this.length; i++) {
if(!name) {
if(this[i] === val) return i;
}else{
if (eval('this[i]["'+name+'"]') == val) return i;
}
}
return -1;
};
//全局替换
String.prototype.replaceAll = function(str,str2){
return this.replace(new RegExp(str,"gm"),str2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment