AccessObjectsMethods.md - brainchildservices/curriculum GitHub Wiki

Slide 1

Accessing Object Methods

You access an object method with the following syntax:

  objectName.methodName()

You will typically describe fullName() as a method of the person object, and fullName as a property.

The fullName property will execute (as a function) when it is invoked with ().

This example accesses the fullName() method of a person object:

Slide 2

Example

 <!DOCTYPE html>
 <html>
 <body>

 <h2>JavaScript Objects</h2>
 <p>Creating and using an object method.</p>
 <p>A method is actually a function definition stored as a property value.</p>

 <p id="demo"></p>

 <script>
 const person = {
   firstName: "John",
   lastName: "Doe",
   id: 5566,
   fullName: function() {
     return this.firstName + " " + this.lastName;
   }
 };

 document.getElementById("demo").innerHTML = person.fullName();
 </script>
 </body>
 </html>

Slide 3

image

Slide 4

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

Example

 <!DOCTYPE html>
 <html>
 <body>

 <h2>JavaScript Objects</h2>
 <p>An object method is a function definition stored as a property value.</p>
 <p>If you access it without (), it will return the function definition:</p>

 <p id="demo"></p>

 <script>
 const person = {
   firstName: "John",
   lastName: "Doe",
   id: 5566,
   fullName: function() {
     return this.firstName + " " + this.lastName;
   }
 };

 document.getElementById("demo").innerHTML = person.fullName;
 </script>
 </body>
 </html>

Slide 4

//Output

image

⚠️ **GitHub.com Fallback** ⚠️