Return type of assignment expression using a setter

RESOLVED DUPLICATE of bug 312354

Status

()

Core
JavaScript Engine
RESOLVED DUPLICATE of bug 312354
10 years ago
10 years ago

People

(Reporter: Norris Boyd, Unassigned)

Tracking

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

10 years ago
Mail from Steve Yegge:

Case #1:  setter returns a value - SpiderMonkey returns that value; Rhino returns the first parameter to the setter (i.e. makes it look like an assignment expression).

Rhino:
js> var foo = {get x(){return this.x_}, set x(val){this.x_ = val; return "done"}}
js> var result = (foo.x = 6);
js> result
6

SpiderMonkey:
js> var foo = {get x(){return this.x_}, set x(val){this.x_ = val; return "done"}}
js> var result = (foo.x = 6);
js> result
"done"

Case #2:  setter doesn't return a value - SpiderMonkey returns undefined; Rhino
still returns the first parameter to the setter:

Rhino:
js> var bar = {get x(){return this.x_}, set x(val){this.x_ = val;}}
js> var result = (bar.x = 10)
js> result
10

SpiderMonkey:
js> var bar = {get x(){return this.x_}, set x(val){this.x_ = val;}}
js> var result = (bar.x = 10)
js> result
js> typeof result
undefined

I think Rhino's behavior actually makes more sense, but I've been assuming it
worked like SpiderMonkey and have been returning values (sometimes different
from the assignment parameter) from my setters and assuming it'll work.

---------------------

I'll change Rhino if you think SpiderMonkey's behavior is correct. However, I agree with Steve that the current SpiderMonkey behavior is at least counterintuitive. It's odd that if o.p is a setter then we get the result of o.p = v that can be neither v nor o.p after the assignment. This doesn't match the behavior of the non-setter case.
We're changing this in bug 312354, as far as I know, particularly since ES4 does the same.
Status: NEW → RESOLVED
Last Resolved: 10 years ago
Resolution: --- → DUPLICATE
Duplicate of bug: 312354
You need to log in before you can comment on or make changes to this bug.