-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-frills.js
46 lines (37 loc) · 882 Bytes
/
js-frills.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
Number.prototype[Symbol.iterator] = function*(){
for (let i=0; i<this.valueOf(); i++){
yield i
}
}
Object.defineProperty(Number.prototype, "times", {
get(){
if (!this._times){
this._times = []
for (let i=0; i<this.valueOf(); i++) this._times.push(i)
}
return this._times
}
})
Array.prototype.do = function(fn){
return this.forEach(fn)
}
Array.prototype.getRandom = function(){
return this[parseInt(Math.random() * this.length)]
}
Array.prototype.random = Array.prototype.getRandom
Object.defineProperty(Array.prototype, "first", {
get(){
return this[0]
},
set(){
throw new Error("The `first` property of an array is read-only!")
},
})
Object.defineProperty(Array.prototype, "last", {
get(){
return this[this.length - 1]
},
set(){
throw new Error("The `last` property of an array is read-only!")
},
})