Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a return statement do inside a setter?

I have the following code:

var o = {};
o.a = 1;

var _value = 1;
Object.defineProperty(o,"a",{
    set: function(value){
        _value = value + 1;
        console.log("log: ", value, _value);
        return _value;
    },
    get: function(){
        return _value;
    }
});

In the setter method, I increment _value by one before returning it. So if I set o.a = 5, I would expect the console to print 6 (even though I realize that returning a value from setter doesn't make sense, usually). However, as the following snippet shows, the console prints 5:

> o.a = 5;
log: 5 6 // from console.log;
5 // return; why does it == value and not value + 1?
> o.a;
6
> var i = o.a = 5;
> i;
5
> o.a;
6

So my question is why does it return 5 and not 6?

I hope this is not because I made some silly mistake in the code.

like image 413
WacławJasper Avatar asked Oct 27 '14 06:10

WacławJasper


3 Answers

You cannot return anything from a property setter, and are just getting the value of the assignment (which is 5 because you assigned 5).

what does return in a setter do?

Nothing. Better remove that return statement. At best it's ignored.

like image 88
Thilo Avatar answered Sep 22 '22 15:09

Thilo


Lets look at the simple assignment:

11.13.1 Simple Assignment ( = )

The production AssignmentExpression : LeftHandSideExpression = AssignmentExpression is evaluated as follows:

  1. Let lref be the result of evaluating LeftHandSideExpression.
  2. Let rref be the result of evaluating AssignmentExpression.
  3. Let rval be GetValue(rref).

So rval is assigned the value that is going to be assigned to left hand side (o.a). In your case 5.

  1. Throw a SyntaxError exception if the following conditions are all true: [...]
  2. Call PutValue(lref, rval).

This is where the value is assigned to the left hand side (PutValue(o.a, 5)). As you can see, nothing is done with whathever PutValue returns (it doesn't return anything).

  1. Return rval.

It simple returns the value that was assigned, in your case 5.


The assignment expression always returns the value that was assigned.

like image 20
Felix Kling Avatar answered Sep 22 '22 15:09

Felix Kling


The value of an assignment expression is always equal to the value being assigned:

var a;
console.log(a = 7); // logs 7

This is true no matter whether the thing you are assigning to is a variable, plain property, setter, read-only property, or whatever:

var a = {};
Object.defineProperty(a, "b", { value: 8, writable: false });
console.log(a.b = 9);  // logs 9
console.log(a.b);      // logs 8, because a.b is read-only

As you said yourself, a return value in a setter is meaningless, so when you used a return value in your setter, why did you expect it to actually do anything special? To answer your question "What does a return statement do inside a setter?" - Basically nothing. It evaluates the expression to the right of the return and then throws away the result.

like image 25
JLRishe Avatar answered Sep 21 '22 15:09

JLRishe