简体   繁体   English

存储/导入变量和函数处理(JS)

[英]Store/Import variables & functions Processing(JS)

Stack Overflow! 堆栈溢出! I wanted to store variables in another file so that I would load the variables in file 1, and draw the scene in file 2, ex. 我想将变量存储在另一个文件中,以便将变量加载到文件1中,并在文件2中绘制场景,例如。

closet.js closet.js
var message = "Hello there";

drawer.js drawer.js
draw = function() { text(message, 100, 100); };

So I would do something like that, but instead of importing the files like this; 所以我会做类似的事情,而不是像这样导入文件。

<canvas data-processing-sources="closet.js drawer.js"></canvas>

I wanted to be able to include them in file 2, sort of like this; 我希望能够将它们包含文件2中,就像这样;

closet.js closet.js
var message = "Hello there";

drawer.js drawer.js
import("closet.js"); draw = function() { text(message, 100, 100); };

Is there a way to do this without including them in the HTML file itself? 有没有办法将它们不包含在HTML文件本身中呢? Thanks in advance :) 提前致谢 :)

Check out this question , which yours might even be a duplicate of. 看看这个问题 ,您可能甚至会重复。 It lists several ways to do this in JavaScript, including using JQuery or dynamically adding the <script> tags to the <head> of your page. 它列出了在JavaScript中执行此操作的几种方法,包括使用JQuery或将<script>标记动态添加到页面的<head>

But if you're using the Processing editor, you have another option: use classes by creating a new tab in the Processing editor. 但是,如果您正在使用“处理”编辑器,则还有另一个选择:通过在“处理”编辑器中创建一个新选项卡来使用 For all your purposes, you can treat this as a separate file. 为了您的所有目的,您可以将此视为一个单独的文件。 So lets say you created a separate Closet tab with a message variable. 假设您使用message变量创建了一个单独的“ Closet选项卡。 Your main sketch code might look like this: 您的主要草图代码可能如下所示:

Closet c = new Closet();
draw = function() {
  text(c.message, 100, 100);
};

I think this is probably the way to go. 我认为这可能是要走的路。 It seems like you're trying to over-engineer a solution: either include the files in the html (this is what 99% of all JavaScript code does) or use a class (this is what 99% of all Processing code does). 似乎您正在尝试过度设计解决方案:要么将文件包含在html中(这是所有JavaScript代码的99%所做的事情),要么使用一个类(这是所有Processing代码的99%所做的事情)。

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

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