Example:
JavaScript
x
7
1
// module "my-module.js"
2
export default function func1() {
3
4
func2();
5
6
}
7
where func2 is only available in the file where we do:
JavaScript
1
8
1
import func1 from './my-module.js'
2
3
function func2() {
4
console.log('OK');
5
}
6
7
func1();
8
Is this possible?
Advertisement
Answer
No, func2
must be defined when you create a func1
, otherwise it will be undefined
and will throw a runtime exception when func1
will be invoked.
You can pass func2
as an argument of func1
and invoke it inside.
JavaScript
1
5
1
// module "my-module.js"
2
export default function func1(callback) {
3
callback();
4
}
5
JavaScript
1
8
1
import func1 from './my-module.js';
2
3
function func2() {
4
console.log('OK');
5
}
6
7
func1(func2);
8