I am learning Javascript from udemy and in one lecture tutor has implemented a function using bind() method, however when i am trying to use call() it doesn't work.
Here is a small program that works with bind:
function mapForEach(arr, fn){
var newArr = [];
for (var i=0; i < arr.length; i++){
newArr.push(
fn(arr[i])
)
};
return newArr;
}
var arr1 = [1,2,3];
/* trying to compare if the item is greater than given limiter*/
var checkPastLimit = function(limiter, item){
return item > limiter;
}
/*bind both functions and check if item is greater than 1*/
var arr4 = mapForEach(arr1, checkPastLimit.bind(this,1));
/*print array in boolean*/
console.log(arr4);
-----
Output: [false,true,true]
This program works as I want, however how can i use call() or apply() method here instead of bind? I am trying from yesterday but not getting what i want.
What I tried
I tried following documentation on mozilla's offical page, however I don't get the desired output.
I tried editing the checkPastLimit function like below:
var checkPastLimit = function(limiter, item){
mapForEach.call(arr1, checkPastLimit(this,1));
return item > limiter;
}
console.log(checkPastLimit);
but this code throws recursion problem.
I also tried another trick but it didnt worked too
var checkPastLimit = function(limiter, item){
return item > limiter;
}
mapForEach.call(arr1, checkPastLimit(this,1));
What I Want
The working program listed above generates the desired output using bind(), but i want to get the output using call()
[false,true,true] //if 1 is limiter
[false,false,true] // if 2 is limiter