-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolyfill.js
More file actions
59 lines (42 loc) · 1.41 KB
/
polyfill.js
File metadata and controls
59 lines (42 loc) · 1.41 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
//A polyfill is a piece of code (usually written in JavaScript) that implements modern features of the language in older browsers that do not support them natively.
//this is polyfill of forEach
if(!Array.prototype.myForEach){
Array.prototype.myForEach = function(callBack){
originalArray = this // object context (current object k taeraf point krta h)
for(let i = 0 ; i < originalArray.length; i ++){
console.log( callBack(originalArray[i] , i));
}
}
}
let num = [1,2,3,4,5,6,7,8,9]
num.myForEach((num, i )=> "value " + num + " index is " + i )
//polyfill of map
//map signature - it return a new array
if (!Array.prototype.myMap) {
Array.prototype.myMap = function(callBack){
let newArr = []
originalArray = this
for(let i = 0 ; i < originalArray.length; i++){
let irr = callBack(originalArray[i] , i)
newArr.push(irr)
}
return newArr;
}
}
const newArray = num.myMap(v => v)
console.log(newArray);
// polyfill of filter
if(!Array.prototype.myFilter){
Array.prototype.myFilter = function(callBack) {
let value = []
for(let i =0 ; i < this.length ; i++){
if (callBack(this[i])) {
value.push(this[i])
}
}
return value
}
}
let even = num.myFilter(n => n % 3 == 0)
console.log(even);
//polyfill of Promies