Skip to content

Instantly share code, notes, and snippets.

@shirohana
Created October 27, 2017 12:43
Show Gist options
  • Save shirohana/4d774e73425ac916b31718a0f48a6d62 to your computer and use it in GitHub Desktop.
Save shirohana/4d774e73425ac916b31718a0f48a6d62 to your computer and use it in GitHub Desktop.
[Javascript] Traps when destructuring with getter
/**
* 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