This.name


#1

Java Script

How 'this.name' works in below code...?

var phonebookEntry = {};

phonebookEntry.name = 'Oxnard Montalvo';
phonebookEntry.number = '(555) 555-5555';
phonebookEntry.phone = function() {
console.log('Calling ' + this.name + ' at ' + this.number + '...');
};

phonebookEntry.phone();

`


#2

If you're just asking how it works in the first place I can give an explanation...but I'm not sure that's what you mean. Is there something wrong with the code? Doesn't look like it.

Basically, the word "this.name" means the name attribute attached to whatever function mentioned it last.

You can technically use phonebookEntry.name but because you're using the .name value inside of the function it was called you can use this. this function, basically, is how I hthink of it.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.