METHODS OF OBJECTS

JS

fullName is an example of “javascript object methods“.

const person = {
  firstName: “John”,
  lastName: “Doe”,
  id: 5566,
  fullName: function() {
    return this.firstName + ” ” + this.lastName;
  }
};

As for the keyword this, refer to this post (the keyword this). JavaScript methods are actions that can be performed on objects. A JavaScript method is a property that contains a function definition.

Methods

Methods are functions stored as properties of the object.

ACCESS THE METHODS

An object method is accessed with the following syntax:

objectName.methodName()

You usually describe fullName () as a method of the person object and fullName as a property. The fullName property will be executed (as a function) when called with (). This example accesses the fullName () method of a person object:

name = person.fullName();

If you access the fullName property, without (), it will return the function definition:

ADD A METHOD TO AN OBJECT

person.denominazione function () {
  return (this.firstName + ” ” + this.lastName).toUpperCase();
};

Copy to Clipboard

LINKS TO JAVASCRIPT

JAVASCRIPT

LINK TO THE CODE ON GITHUB

GITHUB