-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprototype.js
79 lines (72 loc) · 2.6 KB
/
prototype.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//提示:本方法包中都是向内置类的原型上增加方法,请谨慎使用
(function (pro) {
//unique:Array distinct
pro.unique = function unique() {
var obj = {};
for (var i = 0; i < this.length; i++) {
var cur = this[i];
obj[cur] == cur ? (this[i] = this[this.length - 1], this.length -= 1, i--) : obj[cur] = cur;
}
obj = null;
return this;
};
//myForEach:forEach compatibility
pro.myForEach = function myForEach(callBack, context) {
if (Array.prototype.forEach) {
return this.forEach(callBack, context);
}
for (var i = 0; i < this.length; i++) {
callBack.call(context, this[i], i, this);
}
};
//myMap:map compatibility
pro.myMap = function myMap(callBack, context) {
if (Array.prototype.map) {
return this.map(callBack, context);
}
for (var i = 0; i < this.length; i++) {
this[i] = callBack.call(context, this[i], i, this);
}
return this;
};
})(Array.prototype);
(function (pro) {
//myTrim:Remove the string and space
pro.myTrim = function myTrim() {
return this.replace(/(^ +| +$)/g, "");
};
//mySub:Intercept string, this method is distinguished in English
pro.mySub = function mySub() {
var len = arguments[0] || 10, isD = arguments[1] || false, str = "", n = 0;
for (var i = 0; i < this.length; i++) {
var s = this.charAt(i);
/[\u4e00-\u9fa5]/.test(s) ? n += 2 : n++;
if (n > len) {
isD ? str += "..." : void 0;
break;
}
str += s;
}
return str;
};
//myFormatTime:Format time
pro.myFormatTime = function myFormatTime() {
var reg = /^(\d{4})(?:-|\/|\.|:)(\d{1,2})(?:-|\/|\.|:)(\d{1,2})(?:\s+)(\d{1,2})(?:-|\/|\.|:)(\d{1,2})(?:-|\/|\.|:)(\d{1,2})$/g, ary = [];
this.replace(reg, function () {
ary = ([].slice.call(arguments)).slice(1, 7);
});
var format = arguments[0] || "{0}年{1}月{2}日 {3}:{4}:{5}";
return format.replace(/{(\d+)}/g, function () {
var val = ary[arguments[1]];
return val.length === 1 ? "0" + val : val;
});
};
//queryURLParameter:Gets the parameters in the URL address bar
pro.queryURLParameter = function queryURLParameter() {
var reg = /([^?&=]+)=([^?&=]+)/g, obj = {};
this.replace(reg, function () {
obj[arguments[1]] = arguments[2];
});
return obj;
};
})(String.prototype);