Skip to content

Instantly share code, notes, and snippets.

@oriSomething
Created August 13, 2017 12:58
Show Gist options
  • Save oriSomething/b30d9d4135a6673087a31c3404cf7b3c to your computer and use it in GitHub Desktop.
Save oriSomething/b30d9d4135a6673087a31c3404cf7b3c to your computer and use it in GitHub Desktop.
JavaScript Setters don't effect the expression result as expected
var obj = {
_fooValue: 1,
_fooGetterShouldReturnThis: false,
get foo() {
var value = this._fooGetterShouldReturnThis ? this : this._fooValue;
this._fooGetterShouldReturnThis = false;
return value;
},
set foo(value) {
this._fooGetterShouldReturnThis = true;
this._fooValue = value + 1;
}
}
var foo = (obj.foo = 10);
console.log(foo); // Output `10` instead of `Object { ··· }` or even `11`
console.log(obj.foo); // Output `Object { ··· }` which is `obj`
console.log(obj.foo); // Output `11` which is `obj._fooValue`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment