Function Borrowing in JavaScript: Realizing Functions Donât Belong to Objects
A reflection on function borrowing in JavaScript, explaining how functions are independent from objects and how this connects behavior to data.
Thereâs a moment in JavaScript where something subtle clicks.
You write a method inside an object, call it, and everything behaves as expected. Then someone shows you that same method being used on a completely different object â and it still works.
At first, it feels like a trick.
But itâs not a trick.
Itâs a consequence of how JavaScript actually thinks about functions.
The Illusion of Methods
When we write this:
const person = {
name: "Rishi",
greet() {
return "Hello " + this.name
}
}It looks like greet belongs to person.
Like the function lives inside the object.
But thatâs not really whatâs happening.
A more accurate way to see it is:
the function exists independently
the object just provides context through `this`That distinction is easy to miss â but it changes everything.
Using a Function on Another Object
Now consider:
const anotherPerson = {
name: "Alex"
}
person.greet.call(anotherPerson)This works.
The same function now returns:
"Hello Alex"Nothing was copied.
Nothing was rewritten.
The function didnât change.
Only the value of this changed.
This is what function borrowing really is:
reusing behavior by changing contextthis Is the Real Connector
The key to understanding function borrowing is understanding this.
greet() {
return "Hello " + this.name
}The function doesnât care which object it works on.
It only cares that:
this.name existsSo when you switch this, you switch the meaning of the function.
The behavior stays the same.
The data changes.
Borrowing Through call, apply, and bind
JavaScript gives you explicit tools to control this.
With call:
greet.call(obj)Youâre saying:
run this function now, using obj as thisWith apply, the idea is the same, just with arguments passed as an array.
But bind is different.
const fn = greet.bind(obj)This doesnât run the function.
It creates a new function where this is permanently set to obj.
That difference is easy to overlook, but it leads to an important realization:
call/apply control execution
bind changes the function itselfWhen Binding Becomes Permanent
This is where things get interesting.
function show() {
console.log(this.name)
}
const a = { name: "A" }
const b = { name: "B" }
const fn = show.bind(a)
fn.call(b)You might expect "B".
But the result is:
"A"Because once bind has fixed this, it cannot be overridden.
Even call canât change it anymore.
This reveals a deeper structure:
bind operates at creation time
call/apply operate at execution timeAnd in JavaScript, creation-time decisions win.
Behavior Without Ownership
Function borrowing exposes something fundamental about JavaScriptâs design.
In many languages, methods belong to classes or objects.
In JavaScript:
functions are independent
objects provide contextThat means behavior is not tightly coupled to structure.
A function can operate on any object, as long as that object satisfies its expectations.
This is why something like this works:
const obj = {
0: "a",
1: "b",
length: 2
}
Array.prototype.forEach.call(obj, console.log)The function doesnât care that obj isnât an array.
It only cares that it behaves like one.
A Shift in Perspective
At first, itâs natural to think:
âobjects have methodsâ
But a more accurate way to think is:
objects provide data
functions provide behavior
`this` connects the twoFunction borrowing is simply the act of reusing that behavior with different data.
Why This Matters
This flexibility is part of what makes JavaScript powerful.
It allows:
- reusable logic across different data shapes
- dynamic behavior without rigid structure
- patterns like composition instead of inheritance
But it also introduces complexity.
Because now, understanding a function means understanding:
what it does
and what `this` refers toAnd those are not always obvious.
What Function Borrowing Really Teaches
Function borrowing is not just about call, apply, or bind.
Itâs about realizing something deeper:
Functions in JavaScript are not owned.
They are applied.
And once you see that, many confusing behaviors around this start to make sense.
Because the function was never tied to the object in the first place.
It was only ever using it.