<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: DevLogBook</title>
    <description>The latest articles on Forem by DevLogBook (@dev_logbook).</description>
    <link>https://forem.com/dev_logbook</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3748762%2Fdd386d31-efc9-4e2a-9238-6270ba20dc71.jpg</url>
      <title>Forem: DevLogBook</title>
      <link>https://forem.com/dev_logbook</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/dev_logbook"/>
    <language>en</language>
    <item>
      <title>JavaScript Objects with Methods</title>
      <dc:creator>DevLogBook</dc:creator>
      <pubDate>Wed, 04 Feb 2026 05:52:52 +0000</pubDate>
      <link>https://forem.com/dev_logbook/javascript-objects-with-methods-18mc</link>
      <guid>https://forem.com/dev_logbook/javascript-objects-with-methods-18mc</guid>
      <description>&lt;h2&gt;
  
  
  What Are Methods in Objects?
&lt;/h2&gt;

&lt;p&gt;A function that lives inside an object. Think of an object like a toolbox, and methods are the tools inside it!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Object with data and methods (functions inside!)
let person = {
    name: "Alice",
    age: 25,
    sayHello: function() {
        console.log("Hello!");
    }
};

// Using the method
person.sayHello();  // "Hello!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See? &lt;code&gt;sayHello&lt;/code&gt; is a method - it's a function inside the object!&lt;/p&gt;

&lt;h3&gt;
  
  
  Visual Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Object = Toolbox 🧰
   │
   ├─ Property (data) 📊
   ├─ Property (data) 📊
   ├─ Method (function) 🔧
   └─ Method (function) 🔧

Usage: toolbox.method()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Real-Life Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createAccount(username, initialBalance) {
    let balance = initialBalance;

    return {
        deposit: function(amount){
            balance += amount;
            console.log(`Deposited $${amount}. New balance: $${balance}`);
        },
        withdraw: function(amount){
            balance -= amount;
            console.log(`Withdrew $${amount}. New balance: $${balance}`);
        },
        checkBalance: function(){
             console.log(`Alice's balance: $${balance}`);
        }
    }
}

let account = createAccount("David", 100);
account.deposit(50);       // "Deposited $50. New balance: $150"
account.withdraw(30);      // "Withdrew $30. New balance: $120"
account.checkBalance();    // "David's balance: $120"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Important: NO &lt;code&gt;()&lt;/code&gt; When Defining!
&lt;/h3&gt;

&lt;h5&gt;
  
  
  ❌ WRONG:
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj = {
    method(): function() { }  // Syntax error!
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  ✅ CORRECT:
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj = {
    method: function() { }  // Good!
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using &lt;code&gt;this&lt;/code&gt; Keyword
&lt;/h3&gt;

&lt;p&gt;Methods can access other properties in the same object using &lt;code&gt;this&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let person = {
    name: "David",
    age: 25,

    introduce() {
        console.log(`Hi! I'm ${this.name} and I'm ${this.age} years old.`);
    },

    birthday() {
        this.age++;
        console.log(`Happy birthday! I'm now ${this.age}!`);
    }
};

person.introduce();  // "Hi! I'm David and I'm 25 years old."
person.birthday();   // "Happy birthday! I'm now 26!"
person.introduce();  // "Hi! I'm David and I'm 26 years old."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why Use Methods in Objects?
&lt;/h2&gt;

&lt;h5&gt;
  
  
  ✅ Organization
&lt;/h5&gt;

&lt;p&gt;Group related functions together:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ❌ Messy
function addNumbers(a, b) { return a + b; }
function subtractNumbers(a, b) { return a - b; }
function multiplyNumbers(a, b) { return a * b; }

// ✅ Clean
let math = {
    add(a, b) { return a + b; },
    subtract(a, b) { return a - b; },
    multiply(a, b) { return a * b; }
};

math.add(5, 3);  // Much cleaner!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  ✅ Reusability
&lt;/h5&gt;

&lt;p&gt;Create multiple instances:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createDog(name) {
    return {
        bark: function() {
            console.log(`${name} says: Woof!`);
        }
    };
}

let dog1 = createDog("Buddy");
let dog2 = createDog("Max");

dog1.bark();  // "Buddy says: Woof!"
dog2.bark();  // "Max says: Woof!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  ✅ Data Privacy
&lt;/h5&gt;

&lt;p&gt;Keep data private using closures:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createPassword(pwd) {
    let password = pwd;  // Private variable!

    return {
        check: function(attempt) {
            return attempt === password;
        },

        change:  function(oldPwd, newPwd) {
            if (oldPwd === password) {
                password = newPwd;
                return "Password changed!";
            }
            return "Wrong password!";
        }
    };
}

let myPassword = createPassword("secret123");

console.log(myPassword.password);  // undefined - can't access directly!
console.log(myPassword.check("secret123"));  // true
console.log(myPassword.change("secret123", "newPass"));   // "Password changed!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Quick Comparison
&lt;/h2&gt;

&lt;p&gt;Method in Object vs Regular Function&lt;/p&gt;

&lt;h3&gt;
  
  
  Object Method:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let calculator = {
    add: function(a, b) {
        return a + b;
    }
};

calculator.add(2, 3);  // Must use object.method() =&amp;gt; calculator.___
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Regular Function:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(a, b) {
    return a + b;
}

add(2, 3);  // Call directly
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use object methods when &lt;strong&gt;functions are related and belong together!&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Define Methods
&lt;/h2&gt;

&lt;p&gt;There are 3 ways to write methods:&lt;/p&gt;

&lt;p&gt;1️⃣ Traditional Way&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let calculator = {
    add: function(a, b) {
        return a + b;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2️⃣ Shorthand (Modern - Recommended!)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let calculator = {
    add(a, b) {
        return a + b;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3️⃣ Arrow Function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let calculator = {
    add: (a, b) =&amp;gt; {
        return a + b;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Common Mistakes
&lt;/h2&gt;

&lt;h5&gt;
  
  
  ❌ Mistake 1: Adding &lt;code&gt;()&lt;/code&gt; when defining
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj = {
    method(): function() { }  // Wrong!
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  ❌ Mistake 2: Forgetting &lt;code&gt;()&lt;/code&gt; when calling
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;obj.method;  // Returns the function, doesn't call it!
obj.method();  // Calls the function ✓
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  ❌ Mistake 3: Wrong template literal syntax
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log`Hello`;   // Wrong!
console.log(`Hello`);  // Correct!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Happy Coding...!&lt;br&gt;
&lt;strong&gt;Found this helpful? Give it a ❤️!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>devlogbook</category>
    </item>
  </channel>
</rss>
