简体   繁体   English

使变量字符串成为函数名

[英]making a variable string a function name

I was wondering if there was a way to make a var value a function. 我想知道是否有一种方法可以使var值成为函数。 I'm currently working on a project that makes scripts and runs the code, but when all the function names are the same instead of the two scripts running independent code their writing the same code in unison. 我目前正在开发一个制作脚本并运行代码的项目,但是当所有函数名称都相同而不是两个脚本运行独立代码时,它们会一致地编写相同的代码。 If you could show me how to do this it would be a huge help! 如果您可以向我展示如何做到这一点,那将是巨大的帮助! But I'm not exactly sure if its possible. 但是我不确定是否可能。 None the less heres some code 不过这里有一些代码

 var update = setInterval(function(){ checkDotPop(); sc(); }, 1); var canvas = document.getElementById("canvas"); var body = document.getElementById("body"); totalDots = 2; aliveDots = 1; //styling body.style.border = "0px"; canvas.style.backgroundColor = "black"; function checkDotPop(){ while(aliveDots != totalDots){ makeDot(); aliveDots++; } } function makeDot(){ var scr = document.createElement("script"); scr.setAttribute("id", "dot" + totalDots); document.body.appendChild(scr); var script = document.getElementById("dot" + totalDots); script.innerHTML = "var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); var rand1; var rand2; function changeRand(){rand1 = Math.floor(Math.random() * 300) + 1; rand2 = Math.floor(Math.random() * 300) + 1;} function sc(){ changeRand(); context.fillStyle = 'red'; context.fillRect(rand1, rand2, 10, 10); context.fill();"; } 
 <!DOCTYPE html> <html> <head> </head> <body id="body"> <canvas id="canvas" height="500px" width="500px"/> </body> </html> 

It'll say that "sc is not defined" but in the version I have (using notepad) sc is a gobal function and can be called from script to script 它会说“ sc未定义”,但是在我使用的版本中(使用记事本),sc是一个高级函数,可以在脚本之间调用

You where missing a closing bracket for your sc function. 您错过了sc函数的右括号。

 var update = setInterval(function(){ checkDotPop(); sc(); }, 1); var canvas = document.getElementById("canvas"); var body = document.getElementById("body"); totalDots = 2; aliveDots = 1; //styling body.style.border = "0px"; canvas.style.backgroundColor = "black"; function checkDotPop(){ while(aliveDots != totalDots){ makeDot(); aliveDots++; } } function makeDot(){ var scr = document.createElement("script"); scr.setAttribute("id", "dot" + totalDots); document.body.appendChild(scr); var script = document.getElementById("dot" + totalDots); script.innerHTML = "var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); var rand1; var rand2; function changeRand(){rand1 = Math.floor(Math.random() * 300) + 1; rand2 = Math.floor(Math.random() * 300) + 1;} function sc(){ changeRand(); context.fillStyle = 'red'; context.fillRect(rand1, rand2, 10, 10); context.fill();}"; } 
 <!DOCTYPE html> <html> <head> </head> <body id="body"> <canvas id="canvas" height="500px" width="500px"/> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM