Accessing Object Methods

An object method is a function definition, stored as a property value. You can access an object method with the following syntax:

   objectName.methodName()

HTML file: Displayed by browser:
<body>
<p id="demo"></p>
<script>
  var person = {
    firstName : "John",
    lastName : "Doe",
    fullName : function() {
        return this.firstName + " " + this.lastName;
    }
  };
   document.getElementById("demo").innerHTML = person.fullName();
</script>
</body>

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

HTML file: Displayed by browser:
<body>
<p id="demo1"></p>
<script>
  var person = {
    firstName : "John",
    lastName : "Doe",
    fullName : function() {
        return this.firstName + " " + this.lastName;
    }
  };
   document.getElementById("demo1").innerHTML = person.fullName;
</script>
</body>


BackTable of ContentsNext