[英]Make a local function as global
In Javascript with ProcessingJS. 在Java语言中使用ProcessingJS。
If I define a function outside the draw function, how can I pass this function to the draw function (without copying and pasting inside the draw function)? 如果我在draw函数之外定义一个函数,该如何将该函数传递给draw函数(在draw函数内部不进行复制和粘贴)?
var myFunction = function()
{
code;
}
var draw = function(){
myFunction();
};
How do you make a function global? 您如何使函数成为全局函数?
I use a limited environment, Khan Academy, and only the simplest functions and commands are available. 我在可汗学院使用有限的环境,只有最简单的功能和命令可用。
Thanks for your replies, and sorry to be a beginner. 多谢您的回覆,也很抱歉成为初学者。
You could pass your function as a parameter. 您可以将函数作为参数传递。 For example:
例如:
var myFunction = function() {code;}
var draw = function(f) { f(); };
draw(myFunction);
I use Khan Academy too. 我也用汗学院。 There are strict syntax rules that don't make sense anywhere else.
有严格的语法规则在其他任何地方都没有意义。 To answer the question,
为了回答这个问题,
var myFunction = function () {
/* Stuff */
}, draw = function () {
myFunction();
};
will work just fine. 会很好。
You're using invalid syntax. 您使用的语法无效。 If you want to make a global function and call it inside
draw()
, just declare it like a regular function: 如果要创建一个全局函数并在
draw()
内部调用它,则像常规函数一样声明它:
var myFunction = function () { // <-- note the "=" sign and "function" keyword here
code;
};
var draw = function (){
myFunction();
};
What you are asking for is to call myFunction inside the draw function. 您要的是在draw函数中调用myFunction。
Here is how you would do that: 这是您要执行的操作:
var myFunction = function(){
//your code here
}
var draw = function(fun){
fun();
}
Basically, if you want to call myFunction() inside the draw() function, you have to pass a function into draw(), this is called a parameter. 基本上,如果要在draw()函数中调用myFunction(),则必须将一个函数传递给draw(),这称为参数。 Like so:
var draw = (fun)
Here, fun is a function being passed into draw. 像这样:
var draw = (fun)
在这里,fun是传递给draw的函数。 Now you just need to call it using fun(); 现在您只需要使用fun()来调用它;
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.