-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
125 lines (94 loc) · 2.66 KB
/
Copy pathutils.js
File metadata and controls
125 lines (94 loc) · 2.66 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
String.prototype.trim = function(){
return this.replace(/(^\s+)|(\s*$)/g,"");
};
function getParam(param) {
var rep = new RegExp('(^|&)' + param + '=([^&]*)($|&)');
var ret = location.search.slice(1).match(rep);
return ret ? decodeURIComponent(ret[2]) : '';
}
function getParam1(param) {
var query = location.search.slice(1);
var queryArray = query.split('&');
var obj = {};
queryArray.forEach(function(item) {
var ret = item.split('=');
if (ret[0]) {
obj[ret[0]] = decodeURIComponent(ret[1]) || '';
}
});
return param ? obj[param] : obj;
}
// 数组去重
Array.prototype.unique = function() {
var obj = {},
k,
ret = [];
this.forEach(function(v) {
if (typeof v == 'number') {
k = 'n' + v;
} else {
k = v;
}
if (!obj[k]) {
ret.push(v);
obj[k] = true;
}
});
return ret;
};
Array.prototype.unique1 = function() {
var obj = {},
type,
ret = [];
this.forEach(function(v) {
type = typeof v;
if (!obj[v]) {
ret.push(v);
obj[v] = [type];
} else if (obj[v].indexOf(type) === -1) {
ret.push(v);
obj[v].push(type);
}
});
return ret;
};
function trycatch(fn, handle) {
try {
return fn();
} catch (err) {
return handle(err);
}
}
// 不使用loop循环,创建一个长度为100的数组,并且每个元素的值等于它的下标?
// 方式1 利用字符串的类数组对象特性
Object.keys(" ".repeat(100)).map(Number);
// " ".repeat(100) 可以换成 new Int32Array(100),Int32Array是会生成空数组
Object.keys(new Int32Array(100)).map(Number);
// 利用Number
Array.apply(null, {length: 100}).map(Number.call);
// keys把空数组转换成迭代器,扩展运算符执行迭代器生成数组
[...new Array(100).keys()]
// 通过fill初始化空数组, map把key赋给value
new Array(100).fill().map((_,i) => i);
// 通过Array.from初始化空数组
Array.from(new Array(100)).map((_,k) => k);
// 方式2 递归
var arr=[];
function fn(n) {
if ( n != 0 ) {
arr[n] = n;
fn(n - 1)
}else{
arr[0] = 0;
}
}
fn(99);
console.log(arr);
(function() {
// 类数组对象的转换
// 方式1:利用Array构造函数特性
let args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments));
// 方式2:利用Array.prototype.slice方法,
// 对参数使用slice会阻止某些JavaScript引擎中的优化 比如 V8 引擎
let args1 = Array.prototype.slice.call(arguments);
})();