[F2016061803] Considerations for module Introduction and variable deconstruction of ES6

Source: Internet
Author: User

In ES6, the variable deconstruction is this:

Const A = {b:1}const {b} = a

We can use the destructor assignment directly to get the property of the object with the same name, which is equivalent to:

Const B = a.b

In addition to the destructor assignment of variables, ES6 's module import also provides similar syntax:

Import {Resolve} from ' path '

If you use Webpack to build a project, be aware that the deconstruction here is different from the normal variable, such as the following code in A.js:

Export default {    b:1}

If the package is imported according to the destructor of the normal variable, this is the form:

Import {B} from './a '

It is possible that an error occurs and the variable B cannot be exported.

Mainly because this is related to the construction of Webpack. When using module import, when built with Webpack, the above

Import {B} from './a '

into a similar

_a.default.b

You can see that the variable B is on the _a.default, not on the _a, so the deconstruction is undefined. If you want to deconstruct correctly, you must export within the module, namely:

Export Const B = 1

In this case, in the constructed code, the variable B is on the _a, not on the _a.default, so that it can be properly deconstructed.

This is also the use of webpack need to pay attention to the problem, otherwise the runtime will find that clearly is the correct syntax, why export is undefined it? Remember: When using

Import {B} from './a '

A similar syntax, variable B needs to be actively exported.

[F2016061803] Considerations for module Introduction and variable deconstruction of ES6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.