Thursday, October 31, 2019

javascript - Both call and apply produce the same result for String.prototype.toUppercase when passing an array

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

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...