调用在函数中定义的函数

时间:2022-07-06 00:00:12

*Is there a way to call a function defined inside another function in javaSCRIPT? For example:

*是否有方法调用javaSCRIPT中定义在另一个函数中的函数?例如:

window.onload() = function() {
    function my_function(){
        print("Blah");
    };
};

function function_two(){
    my_function();
};

Is there a way to do something like the above (calling my_function in function_two even though it's defined inside the window.onload() function)? In my actual code, which also uses the raphael.js library, I'm trying to write a button in HTML, which using the onClick function, calls a function(like function_two) that runs the function defined in window.onload() (like my_function). However the console says that the my_function is undefined.

是否有一种方法可以执行上面的操作(在function_two中调用my_function,即使它是在window.onload()函数中定义的)?在我的实际代码中,它也使用拉斐尔。我正在用HTML编写一个按钮,该按钮使用onClick函数调用一个函数(如function_two),该函数运行windows .onload()(如my_function)中定义的函数。但是控制台说my_function没有定义。

3 个解决方案

#1


12  

The scope of the function is the core issue here, as Zeychin and Trevor have said. I thought I'd offer another way of handling it. Basically, you can set your function to a variable that's in a higher scope (that is, accessible to both the onload and function_two functions), while defining it inside the onload function as you originally have:

正如Zeychin和Trevor所说,函数的范围是这里的核心问题。我想我可以提供另一种处理方法。基本上,你可以将你的函数设置为一个更大范围内的变量(即onload和function_two函数都可以访问),同时在onload函数中定义它,就像你最初拥有的那样:

var myFunction; //This is the placeholder which sets the scope

window.onload() = function() {
   myFunction = function() { //Assign the function to the myFunction variable
      print('blah');
   }
}

function function_two() {
   myFunction();
}

This might be handy if you only know the information you need for myFunction once you're in the onload event.

如果在onload事件中只知道myFunction所需的信息,那么这可能很方便。

#2


3  

You can not do what you are asking to do. The function my_function()'s scope is only within the anonymous function, function(). It falls out of scope when the method is not executing, so this is not possible. Trevor's answer is the way to do this.

你不能做你要做的事。函数my_function()的作用域仅在匿名函数function()中。当方法不执行时,它就会超出范围,所以这是不可能的。特雷弗的答案是这样做的。

#3


3  

window.onload = function() {
    my_function()
};

function my_function(){
    alert("Blah");
};

function function_two(){
    my_function();
};

#1


12  

The scope of the function is the core issue here, as Zeychin and Trevor have said. I thought I'd offer another way of handling it. Basically, you can set your function to a variable that's in a higher scope (that is, accessible to both the onload and function_two functions), while defining it inside the onload function as you originally have:

正如Zeychin和Trevor所说,函数的范围是这里的核心问题。我想我可以提供另一种处理方法。基本上,你可以将你的函数设置为一个更大范围内的变量(即onload和function_two函数都可以访问),同时在onload函数中定义它,就像你最初拥有的那样:

var myFunction; //This is the placeholder which sets the scope

window.onload() = function() {
   myFunction = function() { //Assign the function to the myFunction variable
      print('blah');
   }
}

function function_two() {
   myFunction();
}

This might be handy if you only know the information you need for myFunction once you're in the onload event.

如果在onload事件中只知道myFunction所需的信息,那么这可能很方便。

#2


3  

You can not do what you are asking to do. The function my_function()'s scope is only within the anonymous function, function(). It falls out of scope when the method is not executing, so this is not possible. Trevor's answer is the way to do this.

你不能做你要做的事。函数my_function()的作用域仅在匿名函数function()中。当方法不执行时,它就会超出范围,所以这是不可能的。特雷弗的答案是这样做的。

#3


3  

window.onload = function() {
    my_function()
};

function my_function(){
    alert("Blah");
};

function function_two(){
    my_function();
};