Javascript call method example
var john = {
name:'John',
age:28,
presentatoin:function(style,timeOfDay){
if(style == 'formal'){
console.log(this.name + this.age + timeOfDay)
}
if(style == 'friendly'){
console.log(this.name + this.age + timeOfDay)
}
}
}
john.presentatoin('formal','Goodmorning')
var emily = {
name:'Emily',
age:23,
}
john.presentatoin.call(emily,'friendly','Evening')
Javascript bindmethod example
Bind doesn’t immediately call the function we can store it in variable somewhere it is useful with function in preset arguments.Now we don’t have to define ‘formal’ again and again we will store this(formal) in in johnFriendly variable.
var johnFriendly = john.presentatoin.bind(john,'formal')
johnFriendly('morning');
johnFriendly('Evening');
Full example
var years = [1990, 1965 ,1937, 2005, 1998];
function arrayCalc(arr, fn){
var arrRes = [];
for(i =0; i < arr.length; i++){
arrRes.push(fn(arr[i]));
}
return arrRes;
}
function calculateAge(el){
return 2016 - el;
}
function isFullAge(limit,el){
return el >= limit;
}
var ages = arrayCalc(years, calculateAge);
var fullJapan = arrayCalc(ages, isFullAge.bind(this,20))
console.log(fullJapan)