javascript - Is it possible to export a function that calls another function defined in the file where the module is imported from?
问题描述
Example:
// module "my-module.js"
export default function func1() {
...
func2();
...
}
where func2 is only available in the file where we do:
import func1 from './my-module.js'
function func2() {
console.log('OK');
}
func1();
Is this possible?
解决方案
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.
// module "my-module.js"
export default function func1(callback) {
callback();
}
import func1 from './my-module.js';
function func2() {
console.log('OK');
}
func1(func2);
推荐阅读
- javascript - 每行中的dataframe js计算
- java - 如何从一个二维数组中添加整数并创建一个新的二维数组
- python-3.x - 在python中动态命名函数
- spring-boot - 如何编写需要通过 Rest body 进行授权服务调用的 WebFlux 过滤器
- c - 如何重构具有不同结构指针作为参数的函数?
- c# - 为什么 asp.net core 的 URL Rewriting 中间件不匹配下面的正则表达式?
- google-sheets - 带有 INDEX MATCH 的数组公式返回匹配数组
- c++ - 如何创建 char C++ 类型的指针数组?
- c++ - 打印出数组值时获取垃圾值
- r - R Dataframe中列组合的唯一列值