admin管理员组文章数量:1279053
Suppose I have the following code:
class Foo {
constructor() {
this.a = 1;
this.b = 'something';
}
someMethod() {
// Is this legal?
let { a, b } = this;
}
}
Is the destructuring assignment in someMethod
legal?
My gut feeling is that it is fine, but I have seen no reference to this usage in any docs. It currently works in Babel, but presumably because under the hood Babel is transpiling the class into a function. My understanding is that (almost) everything in JS prototypically inherits from Object, so I might expect this to be true for Classes and Class instances too.
The only reference I've seen to what happens under the hood is here and specifies that the JS engine calls the internal method ToObject
which will only throw a TypeError when it encounters null
or undefined
. But the ToObject
docs don't explicitly mention class instances.
Suppose I have the following code:
class Foo {
constructor() {
this.a = 1;
this.b = 'something';
}
someMethod() {
// Is this legal?
let { a, b } = this;
}
}
Is the destructuring assignment in someMethod
legal?
My gut feeling is that it is fine, but I have seen no reference to this usage in any docs. It currently works in Babel, but presumably because under the hood Babel is transpiling the class into a function. My understanding is that (almost) everything in JS prototypically inherits from Object, so I might expect this to be true for Classes and Class instances too.
The only reference I've seen to what happens under the hood is here and specifies that the JS engine calls the internal method ToObject
which will only throw a TypeError when it encounters null
or undefined
. But the ToObject
docs don't explicitly mention class instances.
- unrelated to your question, but do you any particular reason you used let instead of var? – Vlad Nicula Commented Feb 22, 2016 at 11:27
-
yes it is, but
()
afterFoo
isn't – Jaromanda X Commented Feb 22, 2016 at 11:46 -
2
many coders will just use
let
every time because they heard it was a good idea; regardless of whether any blocks are in-play or not... you gotta problem witlet
? – dandavis Commented Feb 22, 2016 at 11:47 - 1 @VladNicula seems like an odd question for a minimal (almost) working example that is clearly designed to show the principal of what the question is about – Jaromanda X Commented Feb 22, 2016 at 11:48
- 2 @all, I was just curios if there's a perf benefit of sorts. It's interesting how most of you got a negative vibe from my question :) – Vlad Nicula Commented Feb 22, 2016 at 13:07
1 Answer
Reset to default 9Destructuring objects is explicitly allowed and is a feature.
this
merely refers to an object. There's nothing special about it.
As long as this
refers to an object, this is absolutely fine. *
* this
may not refer to an object depending on how you call someMethod
, e.g. Foo.someMethod.apply(null)
. But then you really have bigger problems anyway.
本文标签: javascriptIn ES6 is destructuring Class Instance properties permittedStack Overflow
版权声明:本文标题:javascript - In ES6 is destructuring Class Instance properties permitted? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741266175a2368507.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论