Created
October 27, 2017 12:43
-
-
Save shirohana/4d774e73425ac916b31718a0f48a6d62 to your computer and use it in GitHub Desktop.
[Javascript] Traps when destructuring with getter
This file contains hidden or 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
/** | |
* According to the example, we can know that Object-Destructuring | |
* has a left-to-right order (doesn't checked is it related to | |
* transpiler or is standard). | |
*/ | |
class Test { | |
get prop1 () { | |
this._prop2 = 'prop2' | |
return (this._prop1 = 'prop1') | |
} | |
get prop2 () { | |
return this._prop2 | |
} | |
} | |
(() => { | |
const t = new Test() | |
const { prop1, prop2 } = t | |
console.log(prop1, prop2, t) | |
// prop1 prop2 {"_prop2":"prop2","_prop1":"prop1"} | |
})(); | |
(() => { | |
const t = new Test() | |
const { prop2, prop1 } = t | |
console.log(prop1, prop2, t) | |
// prop1 undefined {"_prop2":"prop2","_prop1":"prop1"} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment