admin管理员组

文章数量:1421066

Hi I switched over to ECMAScript-6 javascript syntax a little while ago and am loving it! One thing I noticed and couldn't find a definitive answer on is using nested destructing syntax on an import. What I mean is something like this..

Lets say I have a file that looks like this.

export const SomeUtils = _.bindAll({ //lodash _
    someFunc1(params){
        // .... stuff here
    },
    someFunc2(params){
        // .... stuff here
    },
    someFunc3(params){
        // .... stuff here
    }
});
// ... many more of these

I have been doing something like this to get a specific function

import {Utils} from '../some/path/to/utils';
var {someFunc2} = Utils;

To get to the point.. Is there a way to do a single line import for someFunc2? like how you can do the nested object destruction assignment with brackets? (Aka: {Utils: [{someFunc2}]}) ?

I used to do var someFunc2 = require('../some/path/to/utils').someFunc2; but I can't seem to figure out how to do it with an import statement

Hi I switched over to ECMAScript-6 javascript syntax a little while ago and am loving it! One thing I noticed and couldn't find a definitive answer on is using nested destructing syntax on an import. What I mean is something like this..

Lets say I have a file that looks like this.

export const SomeUtils = _.bindAll({ //lodash _
    someFunc1(params){
        // .... stuff here
    },
    someFunc2(params){
        // .... stuff here
    },
    someFunc3(params){
        // .... stuff here
    }
});
// ... many more of these

I have been doing something like this to get a specific function

import {Utils} from '../some/path/to/utils';
var {someFunc2} = Utils;

To get to the point.. Is there a way to do a single line import for someFunc2? like how you can do the nested object destruction assignment with brackets? (Aka: {Utils: [{someFunc2}]}) ?

I used to do var someFunc2 = require('../some/path/to/utils').someFunc2; but I can't seem to figure out how to do it with an import statement

Share Improve this question edited Dec 12, 2015 at 22:44 John Ruddell asked Dec 12, 2015 at 22:06 John RuddellJohn Ruddell 25.9k7 gold badges59 silver badges88 bronze badges 4
  • Do you mean export class Utils? – Saad Commented Dec 12, 2015 at 22:12
  • @saadq no, its just an object with utility functions in it – John Ruddell Commented Dec 12, 2015 at 22:13
  • Your syntax seems off in that case. Did you mean this? You should have the = before the { and you need to add mas between methods. – Saad Commented Dec 12, 2015 at 22:16
  • @saadq yea sure, its just a simple example I wrote on the fly. but yea. I'll update it with some more details to make it syntactically correct – John Ruddell Commented Dec 12, 2015 at 22:17
Add a ment  | 

2 Answers 2

Reset to default 6

No, ES6 module imports do not provide a destructuring option. The only feature they have are named exports (but without nesting). They are meant to exactly replace your require(…).someFunc2 pattern.


In your specific case, I don't see any reason why you would be exporting a single object as a named export. Just use

export function someFunc1(params){
    // .... stuff here
}
export function someFunc2(params){
    // .... stuff here
}
export function someFunc3(params){
    // .... stuff here
}

so that you then can do

import {someFunc2} from '../some/path/to/utils';

To achieve what you are looking for you need to export as default:

const Utils = _.bindAll({ //lodash _
    someFunc1(params){
        // .... stuff here
    },
    someFunc2(params){
        // .... stuff here
    },
    someFunc3(params){
        // .... stuff here
    }
});

export default Utils;

You can then either import the whole thing of just what you need...

import Utils, { someFunc2 } from '../some/path/to/utils';

本文标签: javascriptECMAScript6 import a nested functionStack Overflow