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.

Share Improve this question edited Feb 22, 2016 at 12:00 baseten asked Feb 22, 2016 at 11:13 basetenbaseten 1,4322 gold badges15 silver badges34 bronze badges 13
  • 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 () after Foo 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 wit let? – 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
 |  Show 8 more ments

1 Answer 1

Reset to default 9

Destructuring 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