Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

75%
+4 −0
Q&A Function call; `this` gets bound to unexpected value

Why parenthesis don't work as you expect You seem to have a rough idea how the this keyword is resolved, so I'll skip explaining that and go straight to your question. I was surprised to find that...

posted 3y ago by Moshi‭

Answer
#1: Initial revision by user avatar Moshi‭ · 2020-09-01T19:48:31Z (over 3 years ago)
# Why parenthesis don't work as you expect

You seem to have a rough idea how the `this` keyword is resolved, so I'll skip explaining that and go straight to your question.

> I was surprised to find that when the above function is called, `this` is still bound to `obj`! It seems that here, `(obj.a)` resolves to not just the function, but also holds information about the "boundedness" of the function!
>
> I was especially surprised since I had assumed that any code which declares `let v = <some code>;` and then goes on to use `v` was always interchangeable with using `<some code>` inline, so long as it is wrapped in parentheses.

The [grouping operator (parenthesis)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Grouping) strictly controls the *order* of operations, not the result[^1]. This means that for all intents and purposes, `(obj.a)` is exactly the same as `obj.a`. For illustration, here is another example.

```javascript
obj.b = "hello again"
console.log(obj.b);        // logs "hello again"
(obj.b) = "goodbye again"; // Note the parenthesis around obj.b!
console.log(obj.b);        // logs "goodbye again"
```

---

More technically, neither `obj.a` nor `(obj.a)` resolve to a function; they resolve to a *reference*. This is why both of them are assignable as shown above. This is also why in your example they appear to hold information about the "boundedness" of the function - references in JavaScript contain information such as the parent of the referenced property (what the specification calls a "base value component") and this is part of what determines what `this` gets bound to.

# Misc answers

> * Is this intentional functionality? (I assume so)

Yes.

> * What technical vocabulary can be used to describe this situation? (E.g. "boundedness" is my own invention)

 * `this` binding - How the `this` keyword is bound to a reference.
 * References - JavaScript has a kind of weird way of doing references, so you should probably look at this more in depth.
 * Execution context - This determines how `this` is bound.

> * Where in the spec is this behaviour outlined?

 * `this` keyword: https://tc39.es/ecma262/#sec-this-keyword
 * Grouping operator: https://tc39.es/ecma262/#sec-grouping-operator
 * References: https://tc39.es/ecma262/#sec-reference-specification-type

[^1]: This in itself is sort of an abstraction, but it shows the concept. The *really* technical details are beyond this answer, so go read the spec if you're interested.