Thursday 31 October 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...