codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

29 / 65 topics
28JavaScript Objects29JavaScript Object Methods & this30JavaScript Constructor Functions31JavaScript Prototypes32JavaScript Classes33JavaScript Class Inheritance34JavaScript Getters and Setters
Tutorials/JavaScript/JavaScript Object Methods & this
🌐JavaScript

JavaScript Object Methods & this

Updated 2026-05-12
15 min read

JavaScript Object Methods & this

In the previous topic, we explored how to create and manipulate objects in JavaScript. Now, let's dive deeper into defining methods within these objects and understanding the behavior of the this keyword inside those methods.

Introduction

Methods are functions that are stored as object properties. They allow objects to perform actions or computations. Understanding how methods work and how the this keyword behaves is crucial for effective JavaScript programming, especially when dealing with object-oriented programming (OOP).

In this tutorial, we'll cover:

  • How to define methods inside objects.
  • The behavior of the this keyword in methods.
  • Common pitfalls and best practices.

Defining Methods Inside Objects

Methods are simply functions that are defined as properties of an object. Here's a basic example:

script.js
1const person = {
2name: 'Alice',
3age: 30,
4greet: function() {
5 console.log('Hello, my name is ' + this.name);
6}
7};
8
9person.greet();
Output
Hello, my name is Alice

In the above example, greet is a method of the person object. When we call person.greet(), it logs "Hello, my name is Alice" to the console.

Arrow Functions as Methods

You can also define methods using arrow functions:

script.js
1const person = {
2name: 'Alice',
3age: 30,
4greet: () => {
5 console.log('Hello, my name is ' + this.name);
6}
7};
8
9person.greet();
Output
Hello, my name is undefined

However, using arrow functions as methods can lead to unexpected behavior because arrow functions do not have their own this context. Instead, they inherit the this value from the surrounding scope.

The "this" Keyword

The this keyword in JavaScript refers to the object on which a method is called. Understanding how this behaves is essential for writing correct and maintainable code.

Inside Object Methods

Inside a regular function (not an arrow function), this refers to the object that the method belongs to:

script.js
1const person = {
2name: 'Alice',
3age: 30,
4greet: function() {
5 console.log('Hello, my name is ' + this.name);
6}
7};
8
9person.greet();
Output
Hello, my name is Alice

Inside Arrow Functions

As mentioned earlier, arrow functions do not have their own this context. They inherit the this value from the surrounding scope:

script.js
1const person = {
2name: 'Alice',
3age: 30,
4greet: () => {
5 console.log('Hello, my name is ' + this.name);
6}
7};
8
9person.greet();
Output
Hello, my name is undefined

Using "this" in Nested Functions

When you have nested functions inside a method, this can behave unexpectedly unless you use techniques like arrow functions or storing the outer this value:

script.js
1const person = {
2name: 'Alice',
3age: 30,
4greet: function() {
5 const nestedFunction = () => {
6 console.log('Hello, my name is ' + this.name);
7 };
8 nestedFunction();
9}
10};
11
12person.greet();
Output
Hello, my name is Alice

In the above example, nestedFunction is an arrow function that inherits the this value from greet.

Practical Example

Let's create a practical example to demonstrate object methods and the use of this. We'll build a simple car object with methods to start and stop the engine.

script.js
1const car = {
2make: 'Toyota',
3model: 'Corolla',
4isEngineRunning: false,
5
6startEngine: function() {
7 this.isEngineRunning = true;
8 console.log('Engine started');
9},
10
11stopEngine: function() {
12 this.isEngineRunning = false;
13 console.log('Engine stopped');
14}
15};
16
17car.startEngine();
18console.log(car.isEngineRunning); // true
19
20car.stopEngine();
21console.log(car.isEngineRunning); // false
Output
Engine started
true
Engine stopped
false

In this example, startEngine and stopEngine are methods of the car object. They modify the isEngineRunning property using this.

Summary

  • Defining Methods: Methods are functions stored as properties of objects.
  • The this Keyword: Inside regular functions, this refers to the object that the method belongs to. Arrow functions do not have their own this context and inherit it from the surrounding scope.
  • Best Practices: Use arrow functions for nested functions or when you want to preserve the outer this context.

What's Next?

Now that you understand how to define methods inside objects and how the this keyword works, let's move on to learning about JavaScript constructor functions. Constructor functions are a fundamental part of object-oriented programming in JavaScript and allow you to create multiple instances of an object with similar properties and methods.

Stay tuned for our next tutorial on "JavaScript Constructor Functions"!


PreviousJavaScript ObjectsNext JavaScript Constructor Functions

Recommended Gear

JavaScript ObjectsJavaScript Constructor Functions