ES6 export all values from object

前端 未结 9 1746
梦谈多话
梦谈多话 2020-11-28 07:32

Say I have a module (./my-module.js) that has an object which should be its return value:

let values = { a: 1, b: 2, c: 3 }

// \"export values\         


        
相关标签:
9条回答
  • 2020-11-28 08:11

    You can do lots of stupid thing with javascript. I will leave this quote here from YDKJS book.

    Mentioned page of the book ->

    https://books.google.com.tr/books?id=iOc6CwAAQBAJ&pg=PT150&lpg=PT150&dq=JS+engine+cannot+statically+analyze+the+contents+of+plain+object&source=bl&ots=7v8fMUgwhx&sig=dP3BpY7mEvpvfyxO_koWaXczBWI&hl=en&sa=X&ved=2ahUKEwi4qseXyrDdAhUS-6QKHZYTAEQQ6AEwAHoECAEQAQ#v=onepage&q=JS%20engine%20cannot%20statically%20analyze%20the%20contents%20of%20plain%20object&f=false

    0 讨论(0)
  • 2020-11-28 08:14

    I suggest the following, let's expect a module.js:

    const values = { a: 1, b: 2, c: 3 };
    
    export { values }; // you could use default, but I'm specific here
    

    and then you can do in an index.js:

    import { values } from "module";
    
    // directly access the object
    console.log(values.a); // 1
    
    // object destructuring
    const { a, b, c } = values; 
    console.log(a); // 1
    console.log(b); // 2
    console.log(c); // 3
    
    // selective object destructering with renaming
    const { a:k, c:m } = values;
    console.log(k); // 1
    console.log(m); // 3
    
    // selective object destructering with renaming and default value
    const { a:x, b:y, d:z = 0 } = values;
    console.log(x); // 1
    console.log(y); // 2
    console.log(z); // 0
    

    More examples of destructering objects: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring

    0 讨论(0)
  • 2020-11-28 08:15

    I can't really recommend this solution work-around but it does function. Rather than exporting an object, you use named exports each member. In another file, import the first module's named exports into an object and export that object as default. Also export all the named exports from the first module using export * from './file1';

    values/value.js

    let a = 1;
    let b = 2;
    let c = 3;
    
    export {a, b, c};
    

    values/index.js

    import * as values from './value';
    
    export default values;
    export * from './value';
    

    index.js

    import values, {a} from './values';
    
    console.log(values, a); // {a: 1, b: 2, c: 3} 1
    
    0 讨论(0)
  • 2020-11-28 08:18

    Does not seem so. Quote from ECMAScript 6 modules: the final syntax:

    You may be wondering – why do we need named exports if we could simply default-export objects (like CommonJS)? The answer is that you can’t enforce a static structure via objects and lose all of the associated advantages (described in the next section).

    0 讨论(0)
  • 2020-11-28 08:25

    try this ugly but workable solution:

    // use CommonJS to export all keys
    module.exports = { a: 1, b: 2, c: 3 };
    
    // import by key
    import { a, b, c } from 'commonjs-style-module';
    console.log(a, b, c);
    
    0 讨论(0)
  • 2020-11-28 08:27

    I just had need to do this for a config file.

    var config = {
        x: "CHANGE_ME",
        y: "CHANGE_ME",
        z: "CHANGE_ME"
    }
    
    export default config;
    

    You can do it like this

    import { default as config } from "./config";
    
    console.log(config.x); // CHANGE_ME
    

    This is using Typescript mind you.

    0 讨论(0)
提交回复
热议问题