.toUpperCase() usage and why my snippet does not work?

I am reading through YDKJS series this and object prototype.
At page 2 there is this example that showcased the usage of this .

//function definations
function identify(){
	return this.name.toUpperCase();
}
function speak(){
	var greeting= "Hello I am "+identify.call(this);
	console.log(greeting);
}
// set up 2 names
var me={name: "Kyle"};
var you={name: "Reader"};
//call functions
identify.call(me);
identify.call(you);

speak.call(me);
speak.call(you);

This is when using keyword this to transfer something to upper case and console.log it.

function identify(context){
	return context.name.toUpperCase();
}
function speak(context){
	var greeting ="hello i am "+identify(context);
	console.log(greeting);
}

var me = {name: "Kyle"};
var you = {name: "Reader"};

identify(you);
speak(me);

Code above is how you do it without this .

ANd then I wrote my own version of “not using this” to achieve the same result, but I got a reference Error in chrome. Why is this happening? And what does context mean? why do I need to add a word context to make it work?

var me={name: "Kyle"};
var you={name: "Reader"};

function identify(){
	return name.toUpperCase();
}
function speak(){
	console.log(identify.name);
}

identify(me);
speak(me);

It’s not that the word context is special, it is just that is used in this case as an argument.

I will try to explain it using an example…

function showThisInConsole(argument){
 console.log(argument.toUpperCase());
}

showThisInConsole("hello");

The output in the console should be HELLO.

But if you don’t use/mention/call the argument that you passed to the function, like what you did when you removed the context, than nothing would appear. The “word” you are using inside the brackets doesn’t need to be a real word, it can be whatever you decide it to be.You name it. Here is another example:

function showThisInConsole(veryStrangeArgumentNameAndATypo){
 console.log(veryStrangeArgumentNameAndATypo.toUpperCase());
}

showThisInConsole("hello");

Hope that helps :slight_smile:

that totally helped!!!~

now I know if I want to pass in an argument later when the function is getting executed, I bette name a parameter @ function declaration. No matter what the name is.

Is this correct???

:wink:

By doing this

function showThisInConsole(randomArgumentName){
 console.log(randomArgumentName.toUpperCase());
}

You only define how the function will work. It will output in console the argument passed to it in upper case. When you execute the function with this line here

showThisInConsole("hello");

What you are actualy doing is that randomArgumentName is now “hello”, and than the ouput in console is HELLO.

1 Like

i see… a parameter changed into an argument.