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
-
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
2 Answers
Reset to default 6No, 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
版权声明:本文标题:javascript - ECMAScript-6 import a nested function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745329802a2653759.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论