Methods can use other object properties using the keyword this.
var student = {
'firstName': 'Marie',
'lastName': 'Smith',
'fullName': function() {
return this.firstName + ' ' + this.lastName;
}
}
console.log( student.fullName() ); // The console will show 'Marie Smith'
Important:
Note that functions declared(公開) inside objects don't need names after the keyword. To invoke(召喚) them we use the name of the property, in this case fullName.
function
We can only use the dot notation to invoke methods. We can't do student[fullName()]
console.log('Hello World'); // log is a method of the console object
Math.round(2.7); // round is a method of the Math object
var num = 3;
var num_string = num.toString(); // toString is a method that can be applied to different data types.
document.getElementById("caixa_azul").innerHTML; // getElementById is a method of the document object.
// innerHTML is a property of the object returned by the get method.
var courses = [ "HTML", "Python", "PHP" ];
courses.push("Javascript"); // push is a method of the array data type.
<aside> 🤟🏾 you can learn more here.
</aside>