-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_prototypes.js
More file actions
52 lines (45 loc) · 891 Bytes
/
array_prototypes.js
File metadata and controls
52 lines (45 loc) · 891 Bytes
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
(function(window) {
// standart array declaration
var arr = [1, 2, 3, 4, 5];
// lets make an iterator ?
// first, with function
function iterate(arr) {
var i = 0;
// return a closure that keeps i in memory
return function() {
if(arr.length == i) {
return false;
}
return arr[i++];
}
}
var next = iterate(arr);
var x;
while( x = next()) {
console.log(x);
}
// lets make iterator for all arrays
Array.prototype.iterator = function() {
var self = this;
// this will be the array
var i = 0;
return function() {
if(self.length == i) {
return false;
}
return self[i++];
}
}
console.log("Using prototype");
next = arr.iterator();
while( x = next()) {
console.log(x);
}
// check with hasOwnProperty()
log("Has own property check");
for(var omg in arr) {
if(arr.hasOwnProperty(omg)) {
console.log(omg);
}
}
})(this);