JavaScript object methods

by danielkwood

HTML

<html>
    <head>
        <title>Object methods</title>
        <script src="script.js"></script>
    </head>
        
    <body>

    </body>
</html>

JavaScript

var user = {
    // these are the object properties
    id: 1,
    username: "homersimpson",
    firstName: "Homer",
    lastName: "Simpson",
    suburb: "Springfield",
    // this is an object method
    displayFullName: function(){
        return this.firstName + " " + this.lastName;
    },
    // this is another object method
    displayLocation: function(){
        return this.firstName + " lives in " + this.suburb;
    }
};

// We can call methods on an object
console.log(user.displayFullName());
console.log(user.displayLocation());