javascript - ECMAScript-6 import a nested function? - Stack Overflow

Hi I switched over to ECMAScript-6 javascript syntax a little while ago and am loving it! One thing I n

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';

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745329803a4622825.html

相关推荐

  • javascript - ECMAScript-6 import a nested function? - Stack Overflow

    Hi I switched over to ECMAScript-6 javascript syntax a little while ago and am loving it! One thing I n

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信