I usually see that we should use an array in apply method and array of arguments in call method:
If I do the following (call method would return undefined):
var namelist = {
f:'first name',
l:'last name'
}
function func(a,b){
console.log(this[a]+' '+this[b])
}
func.call(namelist,['f','l'])
//undefined undefined
func.apply(namelist,['f','l'])
//first name last name
But, look here both call and apply method works:
String.prototype.toUpperCase.call(['a','b','c']) //returns 'A,B,C'
String.prototype.toUpperCase.apply(['a','b','c']) //returns 'A,B,C'
Why the call method is working?
If I do use like this:
String.prototype.toUpperCase.call('a','b','c')
//this would return 'A' only.
No comments:
Post a Comment