JavaScript Object Methods — Arrow Functions Break 'this'
A production outage caused 'Name: undefined' due to arrow function methods.
20+ years shipping production JavaScript and front-end systems at scale. Written from production experience, not tutorials.
- Object methods are functions stored as object properties — they let data act on itself
- Use shorthand syntax: methodName() {} instead of methodName: function() {}
- 'this' inside a method refers to the object that owns the method — but only with regular functions
- Arrow functions don't bind their own 'this' — they break method behavior
- Built-in Object.keys(), Object.values(), Object.entries() inspect any object
- Methods show up in Object.keys() — use typeof filter to exclude them from loops
Think of a JavaScript object like a person on a contact card. The card stores data — name, age, phone number. But a real person can also DO things — wave, introduce themselves, calculate their own age from their birthday. Object methods are the 'doing' part. They're functions that live inside an object, giving your data the ability to act on itself. Instead of just storing 'name: Sarah', your object can have a method called 'greet' that says 'Hi, I'm Sarah!' all by itself.
Every app you've ever used is built on data that does things. A user profile doesn't just store a name — it can update itself, calculate a display label, or check whether the account is verified. A shopping cart doesn't just hold items — it can total the price, apply a discount, or check if it's empty. That 'doing' capability is powered by object methods, and it's one of the most important patterns in JavaScript.
Without object methods, you'd write separate functions scattered all over your code — one to greet a user, another to format their name, another to check their status. That gets messy fast. Methods solve this by packaging the behavior directly alongside the data it operates on. The logic lives where it belongs: inside the object itself.
By the end of this article, you'll know exactly how to write methods inside objects, how to use the 'this' keyword to let a method access its own object's data, how to call methods correctly, and how to use JavaScript's powerful built-in object methods like Object.keys(), Object.values(), and Object.entries(). You'll go from 'I sort of get objects' to 'I can actually build something with this.'
What Is an Object Method? Functions That Belong to an Object
A method is just a function — but instead of floating freely in your code, it's attached to an object as one of its properties. That's it. If you already know what a function is, you already know 90% of what a method is.
Here's the mental model: an object has two kinds of properties. Data properties store values — strings, numbers, booleans. Method properties store functions. When a function is stored inside an object, we call it a method.
Why does this matter? Because that method can then use 'this' to refer to the object it's sitting inside. It can read the object's own data, update it, or compute something from it — all without needing anything passed in from outside. The method and the data are teammates living in the same house.
You write a method exactly like you write a normal property, except instead of assigning a string or number as the value, you assign a function. You can use the traditional 'function' keyword or the modern shorthand syntax — both work, and you'll see both in real codebases.
function() {}') is preferred in modern JavaScript. It's less to type, easier to read, and is what you'll see in professional codebases. Use it by default unless you have a specific reason not to.The 'this' Keyword Inside Methods — How a Method Knows Its Own Data
'this' is the word that makes methods genuinely useful. It's a special keyword that, inside a method, points to the object the method belongs to. It's how a method says 'I want to use MY object's data, not some variable from somewhere else.'
Imagine you're an employee at a company. When you say 'my manager', you don't need to explain who you are — 'my' automatically refers to you and your specific context. 'this' works the same way inside a method. When a method says 'this.name', it means 'the name property of the object I belong to.'
This is why methods are more powerful than regular functions for object-related work. A standalone function would need the object passed in as a parameter every single time. A method already knows which object it belongs to, so it can access that data directly.
The key rule to remember for now: inside a method written with 'function' keyword or shorthand syntax inside an object literal, 'this' is the object to the left of the dot when you call the method. 'userProfile.greet()' — 'this' is 'userProfile'. Clear, predictable, useful.
Built-In JavaScript Object Methods — Object.keys(), Object.values(), and Object.entries()
JavaScript ships with several powerful built-in methods that sit on the global 'Object' constructor (capital O). These aren't methods you call on your object — you pass your object into them. They're utility tools for inspecting and working with any object.
Think of them like tools in a toolbox. Your object is a box of stuff. Object.keys() gives you a list of all the labels on the box. Object.values() gives you a list of all the actual contents. Object.entries() gives you both — each label paired with its contents — which is perfect for looping.
Object.keys() returns an array of all the property names (keys) of an object. Object.values() returns an array of all the property values. Object.entries() returns an array of [key, value] pairs. All three ignore inherited properties and only return the object's own directly-defined properties.
These three are genuinely used every day in professional JavaScript. Object.entries() combined with a loop or .map() is one of the most common patterns for transforming data, building UI components, and debugging. Learning them now will pay off immediately.
Object.assign() only does shallow copy — nested objects are shared by reference.JSON.stringify()) is a common fallback but drops functions and undefined.Object.keys(), .values(), .entries() are daily tools for inspecting objects.Adding Methods to Existing Objects and Checking What an Object Can Do
You're not locked in to only the methods you define when you first create an object. JavaScript lets you add new methods to an existing object at any time — you just assign a function to a new property name. This is incredibly flexible and comes up constantly when you're building dynamic applications.
You might start with a plain data object from an API — just properties, no methods. Then in your application code, you can bolt on methods to give it behavior. It's like receiving a plain contact card and then writing your own notes and shortcuts on it.
You can also check whether a method (or any property) exists on an object before trying to call it using 'typeof' or the 'in' operator. This prevents runtime errors in situations where you're not sure what shape an object will have — which happens all the time when working with data from external APIs.
And if you ever need to remove a method or property from an object, the 'delete' operator does exactly that. It permanently removes the property from the object.
Object.keys() just like data properties do. If you want to loop only over data properties (not functions), filter with: Object.keys(obj).filter(key => typeof obj[key] !== 'function'). This is a common real-world pattern when serializing objects.Object.keys().Method Chaining: Return 'this' to Build Fluent APIs
You've seen it in jQuery, Lodash, and even JavaScript's array methods: chaining calls like arr.filter().map().reduce(). You can build the same pattern in your own objects. The trick: each method returns the object itself ('this'), so the next method can be called directly on the result.
Method chaining reduces noise. Instead of nesting functions or storing intermediate results in multiple variables, you chain calls. The code reads left-to-right, like a sentence: 'object.firstMethod().secondMethod().thirdMethod()'.
To enable chaining, every method that's intended to be chained must 'return this;' at the end. Methods that return a value (like a getter) break the chain — you can't call another method on a string or number. Chaining works best with setter-like methods that modify the object and then hand it back.
- Return 'this' from every setter-like method to enable chaining.
- Terminal methods (like
build()or getResult()) return a value and end the chain. - Chaining eliminates intermediate variables — code flows top to bottom.
- Over-chaining can hurt readability if the chain is longer than 5–7 calls.
Object Methods in Practice: Real-World Patterns
You've learned the mechanics. Now let's see where object methods shine in production code. The most common patterns are: factory functions that return objects with methods, methods that coordinate multiple internal calls, and methods that serialize the object for networking.
Factory functions are a clean alternative to classes when you need to create multiple objects with the same behavior. You write a function that returns a new object with methods. Each call creates a fresh object with its own data and the same method definitions. This avoids the confusion of 'this' in classes and is common in React's functional components and in configuration objects.
Another pattern is a method that aggregates data from multiple sources. For example, a 'getSummary' method might concatenate several computed properties instead of requiring the caller to do it. This keeps the formatting logic inside the object, not scattered across the codebase.
Finally, a 'toJSON' method is a JavaScript convention. If an object has a toJSON method, JSON.stringify() will call it automatically. This lets you control exactly how your object is serialized — filter out sensitive fields, rename keys, or flatten structures.
JSON.stringify(). Any object with toJSON will be serialized using that method. Use it to strip sensitive data (passwords, tokens) before sending objects over the network.Object Method Shorthand — ES6 Syntax That Doesn't Waste Cycles
Before ES6, you wrote methods like prisoners chained to a syntax rock: greet: . The colon, the function() { ... }function keyword — it's noise. The machine doesn't care, but your eyes do when scanning a 400-line service object.
ES6 gave us method definitions. No colon. No function. Just the name and parentheses. It's not sugar — it's clarity. When you see vs log() { ... }log: , the first tells you "this is a method that acts on object state." The second screams "somebody copy-pasted from 2011."function() { ... }
Why does this matter in production? Because every byte of cognitive overhead adds up during incident response. You want your code to communicate intent, not ceremonial syntax. If you're using TypeScript, method shorthand also plays nicer with this typing. Less friction when you need to refactor.
One gotcha: method shorthand still binds this dynamically. If you pass that method as a callback without arrow-function wrapping, you'll lose your reference. Know your this rules before you abbreviate.
this to the object. Passing configStore.loadConfig as a DOM event handler? You'll get undefined unless you wrap it in an arrow function or use .bind(this).Updating or Adding a Method to an Object — Mutation You Own
Objects in JavaScript are mutable by default. You can add a method anytime — even after the object is created. This isn't a bug, it's a feature of a dynamic language. But with great power comes responsibility: if you're adding methods to an object that's passed through six different modules, you're now debugging a ghost.
Here's the pattern: obj.newMethod = or function() { ... }obj['newMethod'] = . Works on any object, including instances of classes. But ask yourself: am I augmenting a well-known object (like adding a utility to a config object I control) or am I monkey-patching something that belongs to a library?function() { ... }
If it's the latter, stop. Really. Monkey-patching breaks when the library updates. You'll be the one debugging a Node.js upgrade at 3 AM because your Array.prototype.clear() overload is now colliding with a native method.
Instead, use Object.defineProperty() if you need to control whether the method is enumerable, writable, or configurable. That's the production-grade approach — especially when working with APIs or data that maps from JSON and needs behaviour attached after parse.
Object.prototype pollutes every object — including {} literals. Always check if you're mutating a prototype chain you don't own. If yes, use Object.defineProperty with enumerable: false to keep it invisible during for...in loops.Object.defineProperty for production code to control visibility and protect against accidental override.Production Outage Caused by Arrow Function Methods
- Never use arrow functions to define object methods when they need access to 'this'.
- Enforce this rule with ESLint's 'no-arrow-functions-in-object-properties' custom rule or similar.
- Code review check: if you see an arrow function assigned to a property of an object literal or class, question it.
object.method(). If the method is extracted (e.g., const fn = obj.method), 'this' will be lost — use bind or call.obj.method() not obj.method. The parentheses trigger the function execution.Object.keys() includes methods when you only wanted data propertiesconsole.log(this) inside method to see what 'this' actually isobj.constructor.prototype to verify the method is on the prototype if using classesKey takeaways
Object.keys(), Object.values(), and Object.entries() are your three daily tools for inspecting objectsCommon mistakes to avoid
3 patternsUsing an arrow function as an object method that needs 'this'
greet() {}) or the traditional function keyword (greet: function() {}) for object methods. Only use arrow functions inside methods (e.g. inside a .forEach()), never as the method definition itself.Calling a method without parentheses
Assuming Object.assign() creates a deep copy
Object.assign() only does a shallow copy. Nested objects are still shared by reference. For a true deep copy, use structuredClone(originalObject) (modern JS) or JSON.parse(JSON.stringify(originalObject)) for simple data objects without functions.Interview Questions on This Topic
What is the difference between a function and a method in JavaScript? Can you show me an example of each?
greet() { return 'Hi'; }. A method is a function that is attached to an object as a property. The key difference is that a method has automatic access to the object it belongs to via 'this'. Example: const obj = { name: 'Alice', sayHi() { return 'Hi, ' + this.name; } }; obj.sayHi() is a method.Frequently Asked Questions
20+ years shipping production JavaScript and front-end systems at scale. Written from production experience, not tutorials.
That's JS Basics. Mark it forged?
7 min read · try the examples if you haven't