-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforEach.js
More file actions
73 lines (66 loc) · 1.99 KB
/
forEach.js
File metadata and controls
73 lines (66 loc) · 1.99 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
;"use strict";
var log=function(msg){console.log(msg);};
var alert=function(msg){log(msg);};
if(!Array.prototype.forEach2) {
Array.prototype.forEach2 = function(callback, thisObj) {
if(typeof callback !== "function") {
throw new TypeError("callback should be a function type!!!");
}
var i,
len = this.length;
for(i=0; i<len;i++) {
callback.call(thisObj, this[i], i, this);
}
};
}
if(!Array.prototype.every2) {
Array.prototype.every2 = function(callback, thisObj) {
if(typeof callback !== "function") {
throw new TypeError("callback should be a function type!!!");
}
var i,
len = this.length;
for(i=0; i<len;i++) {
if (false === callback.call(thisObj, this[i], i, this)) {
return false;
}
}
return true;
};
}
if(!Array.prototype.some2) {
Array.prototype.some2 = function(callback, thisObj) {
if(typeof callback !== "function") {
throw new TypeError("callback should be a function type!!!");
}
var i,
len = this.length;
for(i=0; i<len;i++) {
if (true === callback.call(thisObj, this[i], i, this)) {
return true;
}
}
return false;
};
}
//[Testing Codes] -----------------------------------------------------
// forEach
function logArrayElements(element, index, array) {
log("--------------------");
console.log(this.name);
console.log("a[" + index + "] = " + element);
}
[2, 5, 9].forEach2(logArrayElements, {name:'test'});
// some
function isBigEnough(element, index, array) {
return (element >= 10);
}
var passed = [2, 5, 8, 1, 4].some2(isBigEnough);
console.log(passed); // passed is false
passed = [12, 5, 8, 1, 4].some2(isBigEnough);
console.log(passed); // passed is true
// every
passed = [2, 5, 8, 1, 4].every2(isBigEnough);
console.log(passed); // passed is false
passed = [12, 10, 11, 13, 65].every2(isBigEnough);
console.log(passed); // passed is true