这是一个例子。
function parentOne(){
//do something
function ChildOne(){
// do something
};
};
function parentTwo(){
//how can I input data into ChildOne() from here?
};
我正在尝试修复一个错误。 ParentTwo 仅在 childOne 被击中后才起作用。那是因为当在我的代码中点击 childOne 时,它会启动方向 API。之后,我可以使用 ParentTwo 更改表单中的 to 或 from 值,它将提交。这有点难以解释,但如果用户没有首先通过parentOne函数来初始化childOne,我需要parentTwo来初始化childOne。
我知道 javascript,但我以前从未尝试过这个,而且对我来说似乎有点可疑。
最佳答案
您可以将 ParentOne 称为创建函数 (ChildOne) 的工厂。但您需要稍微重写一下。
function parentOne(){
//do something
return function ChildOne(){
// do something
return 'x';
};
};
function parentTwo(){
//how can I input data into ChildOne() from here?
var a = parentOne();
//You can call a() now. It runs the returned function which is ChildOne
var b = a();
//b is now the result of ChildOne, which is 'x'
};
关于Javascript,我可以访问另一个子函数吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26304528/