JavaScript Object Methods: A Complete Tutorial

Learn how to add functions as object methods, use the this keyword correctly, write method shorthand, and avoid common this-binding mistakes.

6 min read

JavaScript object methods are functions that belong to an object. While properties store data about an object (like a user's name or a product's price), methods define what the object can do. A user object might have a greet() method. A cart object might have a calculateTotal method.

Methods turn objects from passive data containers into active entities that carry both state and behavior together.

Defining a Method

There are three ways to add a method to an object. The modern shorthand syntax is the cleanest and most common choice.

Since ES2015, you can define a method by writing the name, parentheses, and body directly inside the object literal, without the function keyword:

javascriptjavascript
const user = {
  name: "Morgan",
  greet() {
    return "Hi, I am " + this.name;
  }
};
 
console.log(user.greet()); // "Hi, I am Morgan"

This is the standard syntax in modern JavaScript. It is shorter, reads more like natural language, and works the same as a regular function.

Traditional Function Property

Before ES2015, you had to write the full function keyword:

javascriptjavascript
const user = {
  name: "Morgan",
  greet: function() {
    return "Hi, I am " + this.name;
  }
};
 
console.log(user.greet()); // "Hi, I am Morgan"

This still works and you will see it in older code. It behaves identically to the shorthand.

Arrow Function Property

You can assign an arrow function, but it comes with a this caveat:

javascriptjavascript
const user = {
  name: "Morgan",
  greet: () => {
    return "Hi, I am " + this.name;
  }
};
 
console.log(user.greet()); // "Hi, I am undefined"

Arrow functions do not have their own this. The this inside the arrow function refers to whatever this was in the surrounding scope when the object was created, which is usually the global object or undefined in strict mode. For object methods, stick with shorthand or regular functions.

The this Keyword Inside Methods

this is the most important concept to understand about methods. Inside a method, this refers to the object that the method is called on:

javascriptjavascript
const counter = {
  count: 0,
  increment() {
    this.count += 1;
    return this.count;
  }
};
 
console.log(counter.increment()); // 1
console.log(counter.increment()); // 2
console.log(counter.increment()); // 3

Each call to increment adds one to the count property and returns the new total. Without this, the method would need to reference the object by its variable name directly, which breaks if you ever rename the variable or reuse the method on a different object:

javascriptjavascript
const counter = {
  count: 5,
  reset() {
    this.count = 0;
    return this.count;
  }
};
 
console.log(counter.reset()); // 0

this makes methods reusable and portable because the same method body works no matter what the object is called.

Methods That Take Arguments

Methods can accept parameters just like standalone functions:

javascriptjavascript
const calculator = {
  result: 0,
  add(n) {
    this.result += n;
    return this.result;
  }
};
 
console.log(calculator.add(5)); // 5

The parameter n works exactly like it would in a standalone function. A second method can read and update the same result property, so the two methods share state through this:

javascriptjavascript
calculator.multiply = function (n) {
  this.result *= n;
  return this.result;
};
 
console.log(calculator.multiply(3)); // 15

Methods That Return the Object (Chaining)

A common pattern is returning this from a method so you can chain multiple method calls together:

javascriptjavascript
const builder = {
  value: "",
  append(str) {
    this.value += str;
    return this;
  },
  uppercase() {
    this.value = this.value.toUpperCase();
    return this;
  }
};

Each method returns this instead of a plain value, so the object itself comes back out of every call. That lets you call the next method directly on the result without storing it in a variable first:

javascriptjavascript
builder.append("hello").append(" world").uppercase();
 
console.log(builder.value); // "HELLO WORLD"

This pattern appears in libraries, DOM manipulation APIs, and builder-style objects.

When this Can Go Wrong

The this binding depends on how a function is called, not where it is defined. This catches many beginners off guard:

javascriptjavascript
const speaker = {
  phrase: "Hello",
  say() {
    return this.phrase;
  }
};
 
const fn = speaker.say;  // Assign the method to a variable
console.log(fn());        // undefined (this is now the global object)

When you call speaker.say(), this is speaker. But when you extract the method and call it as a standalone function through the fn variable, this is no longer speaker. It becomes undefined in strict mode or the global object otherwise.

The fix is to call the method on the object directly, or to bind it:

javascriptjavascript
const boundSay = speaker.say.bind(speaker);
console.log(boundSay()); // "Hello"

For more on this behavior in different contexts, see the article on /javascript/javascript-function-scope-local-vs-global-scope.

Using Methods with Nested Data

Methods often access and transform other properties on the same object:

javascriptjavascript
const cart = {
  items: [{ price: 15 }, { price: 3 }],
  total() {
    let sum = 0;
    for (const item of this.items) sum += item.price;
    return sum;
  }
};
 
console.log(cart.total()); // 18

The total method loops over the items array stored on the same object and adds up each price. A second method can call total through this, so it does not need to repeat that loop:

javascriptjavascript
cart.summary = function () {
  return this.items.length + " item(s), total: $" + this.total();
};
 
console.log(cart.summary()); // "2 item(s), total: $18"

Methods can call other methods on the same object using this.methodName().

Object.keys(), Object.values(), and Object.entries()

JavaScript provides built-in static methods on the Object constructor that help you work with an object's own properties. These are not methods on your object; they are utility functions you call on Object directly:

javascriptjavascript
const book = { title: "Dune", author: "Frank Herbert", year: 1965 };
 
console.log(Object.keys(book));     // ["title", "author", "year"]
console.log(Object.values(book));   // ["Dune", "Frank Herbert", 1965]
console.log(Object.entries(book));
// [["title", "Dune"], ["author", "Frank Herbert"], ["year", 1965]]

These are useful for iterating, transforming, or inspecting objects. For more examples, see the article on /javascript/javascript-object-keys-values-and-entries-guide.

Common Mistakes

Using an arrow function as a method. Arrow functions do not bind this to the calling object:

javascriptjavascript
const obj = {
  name: "Pat",
  greet: () => "Hi " + this.name
};
 
console.log(obj.greet()); // "Hi undefined"

The arrow function's this comes from the surrounding scope, not from the object, so this.name looks in the wrong place. Method shorthand fixes it because a regular method does bind this to the calling object:

javascriptjavascript
const obj2 = {
  name: "Pat",
  greet() { return "Hi " + this.name; }
};
 
console.log(obj2.greet()); // "Hi Pat"

Forgetting to call the method. A method is a function. Accessing it without parentheses returns the function itself, not the result:

javascriptjavascript
const obj = { greet() { return "hello"; } };
console.log(obj.greet);   // [Function: greet] -- not what you want
console.log(obj.greet()); // "hello"

Losing this in callbacks. When you pass a method as a callback, this is lost, because the function that eventually calls it does not know which object it originally belonged to:

javascriptjavascript
const timer = {
  message: "Time is up",
  alert() {
    console.log(this.message);
  }
};
 
setTimeout(timer.alert, 1000); // undefined, this is lost

Passing timer.alert hands setTimeout a bare function reference, so by the time it runs, this no longer points to timer. Wrapping the call in an arrow function keeps the connection to timer intact:

javascriptjavascript
setTimeout(() => timer.alert(), 1000); // "Time is up"

The arrow function wrapper preserves the intended this because it captures timer from the surrounding scope.

Rune AI

Rune AI

Key Insights

  • A method is a function stored as a property on an object, called with obj.method().
  • Use method shorthand syntax: methodName() { ... } inside an object literal.
  • The this keyword inside a method refers to the object the method is called on.
  • Arrow functions do not bind their own this, so avoid them for object methods.
  • Methods let objects carry both data and the behavior that operates on that data.
RunePowered by Rune AI

Frequently Asked Questions

What is the difference between a method and a function?

A function is a standalone block of code. A method is a function that belongs to an object. Methods are called on an object using dot notation: obj.method(). The key difference is that methods have access to the object's data through the this keyword.

Can I use arrow functions as object methods?

You can, but you usually should not. Arrow functions do not have their own this binding, so this inside an arrow method refers to the surrounding scope, not the object the method is called on. Use the method shorthand or regular function syntax instead.

How do I pass arguments to an object method?

Just like a regular function: obj.greet("hello"). Define the method with parameters and pass arguments when you call it.

Conclusion

Object methods are functions that live on objects and operate on the object's own data through the this keyword. Use method shorthand for clean syntax, regular functions when you need a correct this binding, and arrow functions only when you intentionally want to capture the outer this. Mastering methods turns your objects from static data containers into active, self-contained units of behavior.