Created
August 13, 2017 12:58
-
-
Save oriSomething/b30d9d4135a6673087a31c3404cf7b3c to your computer and use it in GitHub Desktop.
JavaScript Setters don't effect the expression result as expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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