gpt4 book ai didi

javascript - 从 JS 中导入的模块内部触发本地函数

转载 作者:行者123 更新时间:2023-12-01 09:42:04 25 4
gpt4 key购买 nike

我正在尝试运行本地函数,但我想从导入的模块内部触发它。这将是它应该如何工作的草图......有什么建议吗?

index.js

import { runBar } from "myModule.js";

runBar();

function foo() {
console.log('foo should run whenever bar is executed');
}

myModule.js

export function runBar(){
bar();
}

function bar() {
console.log("bar is running...");
//I want to call foo from here...
}

最佳答案

foo 作为参数传递怎么样?

index.js

import { runBar } from "myModule.js";

runBar(foo);

function foo() {
console.log('foo should run whenever bar is executed');
}

myModule.js

export function runBar(foo){
bar(foo);
}

function bar(foo) {
console.log("bar is running...");
//I want to call foo from here...
foo();
}

关于javascript - 从 JS 中导入的模块内部触发本地函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58998977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com